- PHP Basics
- Learn PHP
- PHP Comments
- PHP Data Types
- PHP Variables
- PHP Operators
- PHP echo
- PHP print
- PHP echo vs. print
- PHP if else
- PHP switch
- PHP for Loop
- PHP while Loop
- PHP do...while Loop
- PHP foreach Loop
- PHP break and continue
- PHP Arrays
- PHP print_r()
- PHP unset()
- PHP Strings
- PHP Functions
- PHP File Handling
- PHP File Handling
- PHP Open File
- PHP Create a File
- PHP Write to File
- PHP Read File
- PHP feof()
- PHP fgetc()
- PHP fgets()
- PHP Close File
- PHP Delete File
- PHP Append to File
- PHP Copy File
- PHP file_get_contents()
- PHP file_put_contents()
- PHP file_exists()
- PHP filesize()
- PHP Rename File
- PHP fseek()
- PHP ftell()
- PHP rewind()
- PHP disk_free_space()
- PHP disk_total_space()
- PHP Create Directory
- PHP Remove Directory
- PHP Get Files/Directories
- PHP Get filename
- PHP Get Path
- PHP filemtime()
- PHP file()
- PHP include()
- PHP require()
- PHP include() vs. require()
- PHP and MySQLi
- PHP and MySQLi
- PHP MySQLi Setup
- PHP MySQLi Create DB
- PHP MySQLi Create Table
- PHP MySQLi Connect to DB
- PHP MySQLi Insert Record
- PHP MySQLi Update Record
- PHP MySQLi Fetch Record
- PHP MySQLi Delete Record
- PHP MySQLi SignUp Page
- PHP MySQLi LogIn Page
- PHP MySQLi Store User Data
- PHP MySQLi Close Connection
- PHP Misc Topics
- PHP Object Oriented
- PHP new Keyword
- PHP Cookies
- PHP Sessions
- PHP Date and Time
- PHP GET vs. POST
- PHP File Upload
- PHP Image Processing
PHP Basic Syntax
Here you will learn about the basic syntax of PHP language, that is, how to write PHP code.
As you know, a PHP script is executed on the server, and then the plain HTML result is sent back to the browser.
PHP 5 Syntax
You can place your PHP script, anywhere in an HTML document. All PHP script must start with <?php and ends with ?>. In other words all PHP script must be placed inside <?php and ?>. Here is the general form of a PHP script:
<?php // PHP code goes here ?>
And to run the PHP script or PHP code in your browser, you must have to save the file ending with .php extension, and if you save the file ending with other than .php, that is, if you save the file ending with .html or .htm then all the content along with <?php, echo, and ?> etc. will be printed in the browser.
Therefore after writing PHP code, when you save the code in file with any name, then must put .php at the end of that file.
PHP Syntax Example
Let's take an example to understand how PHP code can be written.
<html> <head> <title>PHP Basic Syntax - fresherearth</title> </head> <body> <?php echo "<p>You can use echo to put anything as output.</p>"; echo "<p>This is the way to write any PHP code.</p>"; echo "<p>Save this file as fresherearth.php</p>"; echo "<p>Open browser and type localhost/fresherearth.php</p>"; ?> </body> </html>
Here is the output of the above PHP code, that will be produced in the browser.
Let's look at the following PHP Example:
<!DOCTYPE html> <html> <body> <h1>My first PHP page</h1> <?php echo "Hello PHP!"; ?> </body> </html>
It will produce the following result:
Note - PHP statement ends with a semicolon (;)
PHP Keywords Case Sensitivity
In PHP, all the keywords such as if, else, while, or echo, and classes, functions, or user-defined functions are not case-sensitive. Therefore, here in the below small PHP script, all the three echo statements are valid in PHP:
<!DOCTYPE html> <html> <body> <?php ECHO "Hello World!<br>"; echo "Hello Browser!<br>"; EcHo "Hello PHP!<br>"; ?> </body> </html>
Here is the output produced by the above PHP script:
PHP Variables Case Sensitivity
However, in PHP, all the variable names are case-sensitive. Therefore, here in the below PHP example, only first statement will display the value of the variable named $color. This is because $color, $COLOR, and $coLOR are treated as three different variables.
<!DOCTYPE html> <html> <body> <?php $color = "blue"; echo "My car is " . $color . "<br>"; echo "My house is " . $COLOR . "<br>"; echo "My boat is " . $coLOR . "<br>"; ?> </body> </html>
Here is the output of the above PHP script:
« Previous Tutorial Next Tutorial »