Basic Syntax of PHP

File:PHP-logo.svg - Wikimedia Commons

Hypertext Preprocessor or PHP is a general-purpose script language which is best suited in web developing. It is used for managing databases, session tracking and dynamic contents. It is commonly merged with databases including Oracle, MySQL, Informix, Sybase and many more. PHP is easy to learn since its syntax is basic and beginner-friendly.

PHP script will be executed on the server while the HTML result is sent back to the browser. A PHP script can start with:

<?php 

and end with

?>

Example:

<?php 
// PHP code
?>

A PHP file normally includes HTML tags and the file extension for a PHP file is “.php“. We must remember that programming languages including PHP are case sensitive but in some keywords like if, else, while, echo and user-defined functions are not case sensitive. PHP command ends with a semi-colon (;) like other programming languages.

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

The command echo is used to print the “Hello Nucleio!” and the output should be:

Hello Nucleio!

You can also add some comments inside the script by using // or #.

<?php
# This is a comment.
# It cannot exceed to more lines.

echo "Hello Nucleio!";

// This is a comment.
?>

You can use /* */ for multiple line comment.

<?php
echo "Hello Nucleio!";
/* This is multiple line comment
      This is the 2nd line */
?>

Variables in PHP usually starts with $. In order to start a new line, \n command is used,

<?php
$var1 = 0;
$var2 = 1;
$sum = $var1 + $var2;
echo $sum; 
?>

Leave a Comment

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