Posted on Leave a comment

Crash Course Into PHP – Syntax

[callaction url=”https://www.youtube.com/user/JPlaya01″ background_color=”#333333″ text_color=”#ffffff” button_text=”Go Now” button_background_color=”#e64429″]Subscribe To My Youtube Page[/callaction]

By now you guys should know

That I specialize in Laravel development, it’s what 90% of my blog content comprises of. I want to kick off 2017 with my new online coding classes, these will be Youtube series (mostly screen recordings, but a few face to camera as well you can subscribe to my channel here), designed to teach coding to beginners as well as advance methodologies for more seasoned programmers. The main concern I receive from newcomers coming to my site, is that they would like to see more basic PHP tutorials. As I thought about it, I couldn’t agree more, thus today starts the crash course in PHP lessons. I say crash course because I’m only going over the basic concepts, enough where the Laravel code will start making more sense to the n00bs.

What Is PHP?

PHP is a server-side scripting language designed primarily for web development but also used as a general-purpose programming language. It powers the majority of websites in the world (82.3%). The largest social media platform (Facebook) is powered by PHP, among others. It by far has the most support libraries than any other language. It is free to use and develop with both for personal and commercial applications, making it ideal for programmers with a low budget. PHP code can be directly embedded into HTML, making it ideal to create rich dynamic web pages (and apps).

How To Install PHP

Anyone who knows me, knows I love virtual machines! It makes installing software such as PHP and a full LEMP stack a breeze. Since learning Laravel is the ultimate goal, I will direct you to install the Laravel Homestead virtual machine on your computer, it has the following software already bundled in, meaning you won’t have to do a lick of package management:

  • Ubuntu 16.04
  • Git
  • PHP 7.0
  • Nginx
  • MySQL
  • MariaDB
  • Sqlite3
  • Postgres
  • Composer
  • Node (With PM2, Bower, Grunt, and Gulp)
  • Redis
  • Memcached
  • Beanstalkd

Mac users have the option of using Laravel Valet as well.

PHP Syntax

All PHP files end in .php which shouldn’t be too surprising. Do me a favor, open up your terminal (log into the virtual machine) and create a new php file

nano hello.php

Now paste in the following content and let’s break it down:


<?php
echo "Hello World!";
?>

The first and last parts of this script tell the preprocessor that PHP code needs to be interpreted


<?php
// PHP code goes here
?>

The middle part uses the built-in PHP function (we will get to functions later) echo to print the text “Hello World!” to the screen
The semicolon tells the preprocessor that the PHP statement is completed.
If you run the following command you should see the text “Hello World!” pop up.

php hello.php

The really cool thing about PHP is that you can embed it straight into your HTML, so if you wanted to show this same snippet of code in a webpage instead of the terminal you can simply do this:

HTML Embedding


<!DOCTYPE html>

 

<html>

 

<body>

 

<h1>My first PHP page</h1>

 

<?php echo “Hello World!”; ?>

 

</body>

 

</html>

PHP Comments

Often times when coding you need to leave a note to yourself or fellow programmers. In PHP there are two ways to leave comments in your code

  • // – Creates a SINGLE LINE comment
  • # – Creates a SINGLE LINE comment
  • /**/ – Creates a MULTILINE comment that spans several lines

<?php
// This is a single-line comment
# This is also a single-line comment
/*
This is a multiple-lines comment block
that spans over multiple
lines
*/
?>

This is the most basic syntax of PHP, next we will go over variables and data types. If you haven’t please subscribe to my newsletter to get real time updates when I push new material.