PHP

How PHP works?

PHP is server side scripting language. So, it is execute on the server, and the plain HTML result is sent back to the browser.

PHP File Structure

A PHP script can be placed anywhere in the document.
A PHP script starts with <?php and ends with ?>

Syntax:

<?php
    //PHP code goes here
?>

The default file extension for PHP files is .php.
A PHP file normally contains HTML tags, and some PHP scripting code.
Below, we have an example of a simple PHP file structure, with a PHP script that uses a built-in PHP function "echo" to output the text "Hello PHP!" on a web page:

Example:

<!DOCTYPE html>
<html>
<head>
<title>Hello PHP</title>
</head>
<body>

<?php
    echo "Hello PHP!";
?>

</body>
</html>

Output:

Hello PHP
Hello PHP!

Note:

PHP statements are terminated by semicolon (;).
The closing tag of a block of PHP code also automatically implies a semicolon (so you do not have to have a semicolon terminating the last line of a PHP block).


PHP Tags

The PHP code in the preceding example began with <?php and ended with ?>.
This is similar to all HTML tags because they all begin with a less than (<) symbol and end with a greater than (>) symbol.
These symbols (<?php and ?>) are called PHP tags. They tell the web server where the PHP code starts and finishes.
Any text between the tags is interpreted as PHP. Any text outside these tags is treated as normal HTML.
The PHP tags allow you to escape from HTML.


PHP Tags Style

There are actually four different styles of PHP tags. Each of the following fragments of code is equivalent:

XML style

<?php echo '<p>Tutorialink.com</p>'; ?>

This is the tag style that we use in this site; it is the preferred PHP tag style.
The server administrator cannot turn it off, so you can guarantee it will be available on all servers, which is especially important if you are writing applications that may be used on different installations.
This tag style can be used with Extensible Markup Language (XML) documents.

Recommended:

In general, we recommend you use this tag style.


Short style

<? echo '<p>Tutorialink.com</p>'; ?>

This tag style is the simplest and follows the style of a Standard Generalized Markup Language (SGML) processing instruction.
To use this type of tag-which is the shortest to type-you either need to enable the short_open_tag setting in
your php.ini config file or compile PHP with short tags enabled.

Not Recommended:

The use of this style is not recommended because it will not work in many environments as it is no longer enabled by default.


Script style

<script language='php'> echo '<p>Tutorialink.com</p>'; </script>

This tag style is the longest and will be familiar if you’ve used JavaScript or VBScript.
You might use it if you're using an HTML editor that gives you problems with the other tag styles.


ASP style

<% echo '<p>Tutorialink.com</p>'; %>

This tag style is the same as used in Active Server Pages (ASP) or ASP.NET.
You can use it if you have enabled the asp_tags configuration setting. You probably have no reason to use this style of tag unless you are using an editor that is geared toward ASP or ASP.NET.

Note:

By default, this tag style is disabled.


PHP Statements

You tell the PHP interpreter what to do by including PHP statements between your opening and closing tags.
The preceding example used only one type of statement:
echo '<p>Tutorialink.com</p>';
As you have probably guessed, using the echo construct has a very simple result: It prints (or echoes) the string passed to it to the browser.

Output:

Untitled
Tutorialink.com

PHP Comments

Comments are exactly that: Comments in code act as notes to people reading the code.
Comments can be used to explain the purpose of the script, who wrote it, why they wrote it the way they did, when it was last modified, and so on.
The PHP interpreter ignores any text in comments. Essentially, the PHP parser skips over the comments, making them equivalent to whitespace.
PHP supports three ways of commenting: C, C++, and shell script-style comments.

Example:

<!DOCTYPE html>
<html>
<head>
<title>Comments</title>
</head>
<body>

<?php
    // This is a single line C, C++ type comment

    /*
    This is a multiple lines C, C++ type comment
    block, which is more than one line 
    */

    # This is also a single line shell script style comment
?>

</body>
</html>

Output:

Comments
Comments does not Execute. So, No Output Produced



Subscribe us on Youtube

Share This Page on

Question


amit | 05-Jul-2017 07:36:40 pm

my question is- when i write code in php without php opening and closing tag like "Hello, PHP" then why php doesn't give any error message and give output what is written, like output of the above code is- "Hello, PHP" please somebody help me...??


Ask Question