Launching Soon
HTML
AJAX
JAVA
ASP.NET
 
PHP Intro
PHP Syntax
PHP Variables
PHP Echo
PHP String
PHP Operators
PHP If...Else
PHP Switch
PHP Arrays
PHP While Loops
PHP For Loops
PHP Functions
PHP Forms
PHP $_GET
PHP $_POST
 
PHP Advanced
PHP Date
PHP Include
PHP File
PHP File Create
PHP File Open
PHP File Close
PHP File Write
PHP File Read
PHP File Delete
PHP File Append
PHP File Truncate
PHP File Upload
PHP Cookies
PHP Sesssions
PHP Email
PHP Secure Email
PHP Error
PHP Exception
PHP Filter
 
PHP Database
MY SQL Introduction
MY SQL Connect
MY SQL Create
MY SQL Insert
MY SQL Where
MY SQLOrder By
MY SQL Update
MY SQL Delete
PHP ODBC
 
Extras
PHP Array
PHP Calender
PHP Date
PHP Directory
PHP Error
PHP File System
PHP Filter
PHP FTP
PHP HTTP
PHP Libxml
PHP Mail
PHP Misc

PHP Mysql

PHP SimpleXML
PHP String
PHP XML
PHP Zip
 
 
 
 

php - syntax
Before we talk about PHP's syntax, let us first define what syntax is referring to.

  • Syntax - The rules that must be followed to write properly structured code.


PHP's syntax and semantics are similar to most other programming languages (C, Java, Perl) with the addition that all PHP code is contained with a tag, of sorts. All PHP code must be contained within the following...

PHP Code:


<?php
?>

or the shorthand PHP tag that requires shorthand support to be enabled
on your server...

<?
?>


If you are writing PHP scripts and plan on distributing them, we suggest that you use the standard form (which includes the ?php) rather than the shorthand form. This will ensure that your scripts will work, even when running on other servers with different settings.

how to save your php pages


If you have PHP inserted into your HTML and want the web browser to interpret it correctly, then you must save the file with a .php extension, instead of the standard .html extension. So be sure to check that you are saving your files correctly. Instead of index.html, it should be index.php if there is PHP code in the file.

example simple html & php page



Below is an example of one of the easiest PHP and HTML page that you can create and still follow web standards.


PHP and HTML Code:


<html>
<head>
<title>My First PHP Page</title>
</head>
<body>
<?php
echo "Hello World!";
?>
</body>
</html>

Display:


Hello World!

If you save this file (e.g. helloworld.php) and place it on PHP enabled server and load it up in your web browser, then you should see "Hello World!" displayed. If not, please check that you followed our example correctly.

We used the PHP command echo to write "Hello World!" and we will be talking in greater depth about how echo is special later on in this tutorial.

the semicolon!

As you may or may not have noticed in the above example, there was a semicolon after the line of PHP code. The semicolon signifies the end of a PHP statement and should never be forgotten. For example, if we repeated our "Hello World!" code several times, then we would need to place a semicolon at the end of each statement.

PHP and HTML Code:


<html>
<head>
<title>My First PHP Page</title>
</head>
<body>
<?php
echo "Hello World! ";
echo "Hello World! ";
echo "Hello World! ";
echo "Hello World! ";
echo "Hello World! ";
?>
</body>
</html>



Display:


Hello World! Hello World! Hello World! Hello World! Hello World!



white space
As with HTML, whitespace is ignored between PHP statements. This means it is OK to have one line of PHP code, then 20 lines of blank space before the next line of PHP code. You can also press tab to indent your code and the PHP interpreter will ignore those spaces as well.

PHP and HTML Code:


<html>
<head>
<title>My First PHP Page</title>
</head>
<body>
<?php
echo "Hello World!";

 

echo "Hello World!";

?>
</body>
</html>

 





Display:


Hello World!Hello World!

 


This is perfectly legal PHP code.

 


 


   
 
Designed and Developed by MGEF
PHP MGEF Tutorial is for training only. We do not warrant the correctness of its content. The risk from using it lies entirely with the user.
While using this site, you agree to have read and accepted our terms of use and privacy policy.
Copyright 1999-2010 by MGEF. All Rights Reserved.