In this tutorial, we will be going over some basic syntax of PHP.
PHP Basic Syntax
The default syntax of PHP starts with <?php and ends with ?> Any code inside <?php ?> is PHP code.
Example:
<?php
//PHP code goes in here
echo “Everything inside here is PHP";
?>
Result
If your code contains only PHP code, some people recommend leaving out the coding tag in order to prevent whitespace from accidentally displaying. This is a good idea only if your code contains only PHP.
PHP Statement Separation
In PHP, statements are terminated using semicolons; this is the same as many other programming languages. For the last line of code, the semicolon is optional, as a closing PHP tag implies the end of your code.
Example
<?php
Echo "I am valid code";
?>
<?php
echo "I am also valid code";
?>
Result
Both of the above examples are acceptable syntax.
(as an aside: notice how the output in both lines of code end up in the same line. This is because we did not add any HTML to create a break between the two: just pressing enter is insufficient)
PHP Case Sensitivity
Things such as class, control structures(e.g. if, if else statements) and functions names are not case-sensitive.
Example
<?php
Echo "I am valid";
Echo "I am valid";
echO "I am valid";
?>
Result
All the above are all valid. Variables, however, are case-sensitive.
<?php
$a = "I am not capital A";
$A = "I am not lower case a";
?>
$a
and $A
above are not the same thing.
PHP Whitespace Insensitivity
When you are writing code, it does not matter whether you have white space in between the statements. This has no effect on the output.
Example
<?php
Echo "This is code";
Echo "I have a pressed enter and space a few times but PHP doesn’t care";
?>
Result
PHP Commenting
To comment your code, add # in front of the stuff you want to turn into comments.
Example
<?php
#these are comments the system doesn’t care about
Echo “hello world”;
?>
Result
You can also comment out multiple lines by surrounding all that code with / /
Example
<?php
/*
I am code that no one cares about!
So am I!
And me too!
*/
Echo “Hello world”;
?>
Result