PHP Syntax

In this chapter, you will get an idea about the very basic syntax  of Php. To run php you will need a server which we have already installed in the last chapter.

PHP programs can be written on any editor, such as – Notepad, Notepad++, Dreamweaver, etc. These programs save with .php extension, i.e., filename.php inside the htdocs folder.

For Example : hello.php

In whichever directory you will have installed xampp, you will get a folder named xampp as if I have installed xampp in the Drive D. So, the path for the htdocs directory will be “D:\xampp\htdocs”.

PHP program runs on a web browser such as – Chrome, Internet Explorer, Firefox, etc. follow some steps below

1. create a file inside xampp


simple PHP program like hello world.

    echo "Hello World!";  

The most widely used language construct to print output in PHP is echo

echo "Hello, World!\n"

Alternatively, you can also use print

print "Hello, World!\n";

Both statements perform the same function, with minor differences:

  • echo has a void return, whereas print returns an int with a value of 1
  • echo can take multiple arguments (without parentheses only), whereas print only takes one argument
  • echo is slightly faster than print

Both echo and print are language constructs, not functions. That means they do not require parentheses around their arguments. For cosmetic consistency with functions, parentheses can be included. Extensive examples of the use of echo and print are available elsewhere.

2.Save the file with hello.php

Note : PHP program must be saved in the htdocs folder, Otherwise it will generate an error – Object not found.

3.Now Run the XAMPP server and start Apache and MYSQL

4. Open your Web browser any type this url http://localhost/hello.php on your browser window.

Now you can see the output for the above hello.php program

PHP can also be run from command line directly using the CLI (Command Line Interface).


CLI is basically the same as PHP from web servers, except some differences in terms of standard input and output.


The PHP CLI allows four ways to run PHP code

1. Standard input. Run the php command without any arguments, but pipe PHP code into it: echo ‘<?php echo “hello world!”; | php

2. Filename as argument. Run the php command with the name of a PHP source file as the first argument: php

3. Code as argument. Use the -r option in the php command, followed by the code to run. The echo “Hello world!”; Hello world!

4. Interactive shell. Use the -a option in the php command to launch an interactive shell. Then, type (or paste)
PHP code and hit return : $ php -a Interactive mode enabled php > echo “Hello world!”; Hello world!


Leave a Reply

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