PHP Basic Syntax

  • Author: Sajjad Hossain
  • |
  • Added: Feb 14, 2016
  • |
  • In: Basic PHP

You must create a PHP file (.php) to execute PHP code otherwise, it'll not work. PHP code start with <?php tag and close with ?> tag. And every line complete with a semicolon (; ). Ok now create a php file and open your file in notepad or other IDE (editor) and write down below code

<?php
echo "Hello World! This is my first PHP project";
?>

Now save you page name as hello.php into your local server's htdocs (XAMPP) or www (WAMP) folder. Now open your browser you will get output like this.


You can also use short tags to write PHP like 

<?= "Hello World! This is my first PHP project";?> 
or,

<? "Hello World! This is my first PHP project";?>

or,

<script language="php">

echo "Hello World! This is my first PHP project";

</script>
If you use short tag then you have to configure it into php.ini and enable short_open_tag.
The first way is best for write PHP code and it use most developers.
You can also write PHP code using print . example

<?php
print ("Hello world!");
?>
It'll execute the same result like as echo but echo is better than print.



Top