This article may contain affiliate links. If you buy some products using those links, I may receive monetary benefits. See affiliate disclosure here
This post is aimed at complete beginners taking their first steps in PHP. So let’s discuss the basic syntax.
Syntax is just the grammar rules on how to use a programming language. Learning grammar alone does not make you a good author. Similarly, knowing the syntax of one or two programming languages need not make you a good programmer. But without knowing the syntax you cannot start writing anything functional or meaningful. That’s why learning the syntax is important.
The .php file extension and <?php ?>
tag
So the first thing you need to understand is, PHP code should be placed inside a file ending with .php extension. Whenever the server receives a request to the file, it passes the request to the PHP interpreter to process it. In this case I am using XAMPP. So the Apache server passes the request to the PHP module whenever I try to access the load the script.
Inside the file you should place the PHP code inside PHP tags. This is how it looks:
<?php
//code goes here
?>
The closing tag is not mandatory unless you want to put some non PHP code below that for instance HTML.
Now let’s print a simple hello world message:
<?php
echo "Hello World!";
?>
Note that I ended the statement with a semicolon. Unlike in JavaScript and other some other languages, the ending semicolon is compulsory in PHP. If we put the statement without the opening PHP tags PHP will not interpret it. Instead the whole statement will be treated as plain text.
Case sensitivity
In the echo
statement, the word echo is a built in PHP keyword It’s not case sensitive so you can write it in any way you want Not just the echo
, PHP keywords, functions, and classes are also case insensitive. So the following also works:
<?php
ECHO "Hello World!"; //works
EcHo "Hello World!"; //works
?>
However variable names in PHP are case sensitive.
<?php
$name = "Abhinav";
echo $name; //outputs 'Abhinav'
echo $NAME; //will not work
?>
So the lowercase and uppercase versions are treated as different variables by PHP.
Comments in PHP
Next let’s take a look at PHP comments. It’s always a good practice to add descriptive comments in your code to make it more readable.
To create a single line comment, begin the line with two forward slashes.
<?php
// an example of a comment
// just another line
echo "Hello World!";
?>
You can add as many lines as you want in the same way. But if it’s a long comment, then it’s better to add a multiline comment. Multiline comment begins with a forward slash followed by an asterisk symbol, and ending in asterisk, slash. In between you can write as many lines as you want.
<?php
/*
an example of a multiline comment
comments may contain anything,
even code like echo "hello world",
but it won't be executed - php ignores it
*/
echo "Hello World!"; // another single line comment
?>
Indentation and Curley Brackets
To add blocks of code with multiple statements, put them inside curley brackets. It’s essential when you want to add loops in your code. Here I am creating a simple if loop and adding two echo statements inside the the curly brackets:
<?php
if(true) {
echo "Hello ";
echo "World";
}
?>
And both of them gets printed on the page. Instead if the condition was false, both the statements will be skipped from execution:
<?php
if(false) {
echo "Hello "; //skipped
echo "World"; //skipped
}
?>
Here, you might have noticed that the indentation before the statements inside the brackets. However indentation is not compulsory in PHP. It works even without any indentation at all. However the PHP standards recommendation tells us to use 4 spaces for indentation and nesting.
PHP inside HTML code
There’s a huge reason why beginner web developers embrace PHP. It allows allows adding PHP code inside HTML.
Simply insert the PHP tag and place the code inside that. Here I am out putting a heading tag using the echo
statement.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<h1>Hello World!</h1>
<?php echo '<h2>Welcome to PHP</h2>'; ?> <!-- php inside html -->
</body>
</html>
Not only echo
statements you can add any programming logic inside HTML,although it’s a matter of debate whether you should do it or not as it affects readability.
Here I am going to add two numbers and output the sum of them.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<h1>Hello World!</h1>
<?php echo '<h2>Welcome to PHP</h2>'; ?> <!-- php inside html -->
<?php
$num1 = 4; $num2 = 6;
echo $num1 + $num2; //outputs 10
?>
</body>
</html>
This is an example of an expression with two operands and an operator. Spaces between them are optional. It works with or without spaces.