Our First PHP Script

A PHP script is executed on the server, and the plain HTML result is sent back to the browser.
1. PHP code is written within the <?php and ?> tags
2. The script can be placed any where in the PHP file
3. Generally saved with '.php' extension
4. Version specific extensions like .phtml, .php4, .php5 are also used.


PHP Syntax


Here is a simple syntax:

<?php
    // code statements
?>

PHP Example

The program shown below is a basic PHP application that outputs the words "Hi!!! My first script" When viewed in a web browser.

<?php
    echo "Hi!!! My first script"
?>

Output

Hi!!! My first script



PHP Case Sensitivity


In PHP, keywords (for example, if, else, while, echo, etc.), classes, functions, and user-defined functions are not case-sensitive.However, all variable names are case-sensitive!

<?php
	//Variable names are case-sensitive
	$color = "red";
	echo "My car is " . $color ;
	echo "My house is " . $COLOR ;
	//Keywords are not case-sensitive
	echo "Hello World!";
	EcHo "Hello World!";
?>

Comment in PHP


Both single line and multi-line comments are supported in PHP. For single line comment, we can use # or // before the comment line and for multi-line comments, we use /* ... */. For example,

<?php
	# This is also a single line comment
	// This is a single line comment
	/*
        This is also a single line comment
        another line of comment
        */
?>

Syntax Guidelines and Rules


1. Code comments are usually used to remind you and others what the script is doing.
2. You can shorten and slim down your code a bit by removing all whitespace and newlines.
3. PHP standard variables always begin with a dollar sign($).
4. The semicolon tells the compiler that the line is ending at that point.
5. We use period (.) in PHP to do (join strings or add variable data to strings).