Tuesday, October 23, 2012

PHP Tutorial - Know About PHP Syntax


In earlier post I had stated that PHP is a server side web development tool hence it use to executed on server side only and then the plain HTML result is sent back to the browser.

Thing to remember -
PHP script always starts with <?php and ends with ?>. Although, if your server support shorthand support then you can start scripting with <? and end with ?>. But it is highly recommended that you should go with usual way i.e. <?php and  ?>. Also don't forget to save PHP file in .php extension.

For example:
1. First example using with echo statement;

<html>
        <body>
                <?php
                        echo "hello world!";
                ?>
        </body>
</html>

2. Second example using with print statement;
<html>
        <body>
                <?php
                        print "hello world!";
                ?>
        </body>
</html>

In above example you might have noticed that, each code line in PHP is ending with a semicolon (;). The semicolon act as an separator and is used to distinguished one set of instructions from another.
Now here we had use two different type of statements to output "Hello World" text with PHP "echo" and "print".

Difference between echo and print statement:-
There is very slight difference between two of them which is mentioned below;

1. echo is slightly faster than print. echo is marginally faster since it doesn't set a return value where as print always return 1.

2. Both print and echo take only 1 argument when used with parentless (like a function call). However, when used without parentless, echo can take several argument example: "hello", "world", "!", "repeat".



No comments:

Post a Comment