Syntax In PHP

Learners will benefit from today’s blog post on PHP Syntax. It aims to provide a clear and concise guide with examples to help individuals understand the basics of the PHP language and its syntax. This post is suitable for beginners and those looking to improve their skills.

The PHP script runs on the server and provides the plain HTML result to the web browsers.



PHP Syntax Basics

There is no restriction on where a PHP code can be placed in a document.

There are two parts to a PHP code: the beginning <?php and the end ?>. These tags are the primary structure for any PHP code.

The PHP code must be written between these tags as shown in the examples below:

Example: 

<?php // This is the place where PHP code should go ?>

 

Files written in PHP are saved using the “.php” file extension.

PHP files often include a combination of HTML and PHP code.

The following examples shows two basic PHP script displaying the text “Lionel Messi is a famous footballer” and “Lionel Messi has confirmed that the FIFA 2022 will be his last World Cup ”on a page using the PHP function “echo“:

Example: 

<!DOCTYPE html> <html> <body> <h1>Football</h1> <?php echo " Lionel Messi is a famous footballer"; ?> </body> </html>

 

Example: 

<!DOCTYPE html> <html> <body> <h1>FIFA</h1> <?php echo "Lionel Messi has confirmed that the FIFA 2022 will be his last World Cup "; ?> </body> </html>

Remember: Statements in PHP must end with a semicolon (;).


Case Sensitivity for PHP

PHP does not apply case sensitivity to keywords, classes, functions, or user-defined functions.

This means that “if“, “If“, and “IF” are treated as the same keyword, and the same applies for other keywords, classes, functions, and user-defined functions.

Example: 

<!DOCTYPE html> <html> <body> <?php echo "Football<br>"; ECho "Cricket<br>"; ECHO "basketball<br>"; ?> </body> </html>

Please note that case is sensitive when naming variables!

As you can see in the example below; only the last statement will display the value of the $game variable!.

This is because $Game, $GAME, and $game are treated as three different variables:

Example: 

<!DOCTYPE html> <html> <body> <?php $game = "Cricket"; echo "I Like " . $Game . "<br>"; echo "I Like " . $GAME . "<br>"; echo "I Like " . $game . "<br>"; ?> </body> </html>

 

We value your feedback.
+1
0
+1
0
+1
0
+1
0
+1
0
+1
0
+1
0

Subscribe To Our Newsletter
Enter your email to receive a weekly round-up of our best posts. Learn more!
icon

Leave a Reply

Your email address will not be published. Required fields are marked *