PHP Tutorial

Your cool punch line

PHP Syntax

PHP scripting tag always starts with <?php and ends with ?>. PHP scripting tag can be use anywhere in the document.

There are four sets of tags which can be used to denote blocks of PHP code. Of these, only two (<?php. . .?> and <script language="php">. . .</script>) are always available; the others can be turned on or off from the php.ini configuration file. While the short-form tags and ASP-style tags may be convenient, they are not as portable as the longer versions. Also, if you intend to embed PHP code in XML or XHTML, you will need to use the <?php. . .?> form to conform to the XML.

To activate recognition for short tags, you must make sure that the short_open_tag switch is set to On in php.ini:

short_open_tag = On;

Short tags are enabled by default, so you only need to edit php.ini if you want to disable them.

To activate recognition for the ASP style tags, you must enable the asp_tags setting:

asp_tags = On;

After you have edited php.ini, you should be able to use any of the four styles in your scripts. This is largely a matter of preference, although if you intend to include XML in your script, you should disable the short tags (<? ?>) and work with the standard tags (<?php ?>).

The character sequence <? tells an XML parser to expect a processing instruction and is therefore frequently included in XML documents. If you include XML in your script and have short tags enabled, the PHP engine is likely to confuse XML processing instructions and PHP start tags. Disable short tags if you intend to incorporate XML in your document.

You can use any of the four PHP start and end tags that you have seen:

PHP Example:-

1.) <? echo("if you want to serve XHTML or XML documents, do like this\n"); ?>

2.) <?php echo("this is the simplest, an SGML processing instruction\n""); ?>

3.) <% echo("Hello Web!"); %>

4.) <SCRIPT LANGUAGE="php">
echo("some editors (like FrontPage) don't  like processing instructions!");
</SCRIPT>

Go Top

Instruction Separation

Instructions are separated the same as in C or Perl - terminate each statement with a semicolon.

The closing tag (?>) also implies the end of the statement, so the following are equivalent:

PHP Example:-


<?php
    echo "This is a test";
?>
<?php echo "This is a test" ?>
Go Top

PHP Comments

PHP supports 'C', 'C++' and Unix shell-style comments.

PHP Example:-

<?php
    echo "This is a test"; // This is a one-line c++ style comment
    /* This is a multi line comment
       yet another line of comment */
    echo "This is yet another test";
    echo "One Final Test"; # This is shell-style style comment
?>

The "one-line" comment styles actually only comment to the end of the line or the current block of PHP code, whichever comes first.

<h1>This is an <?php # echo "simple";?> example.</h1>
<p>The header above will say 'This is an example'.
Go Top

PHP Variables

Variables in PHP are represented by a dollar ($) sign followed by the name of the variable. The variable name is case-sensitive.

Variable names follow the same rules as other labels in PHP. A valid variable name starts with a letter or underscore, followed by any number of letters, numbers, or underscores. As a regular expression, it would be expressed thus: '[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*'

Tips:

For our purposes here, a letter is a-z, A-Z, and the ASCII characters from 127 through 255 (0x7f-0xff).
PHP Example:-
<?php
$var = "Raj";
$Var = "Ram";
echo "$var, $Var";      // your outputs "Raj, Ram "

$4website = 'not yet';     // invalid; starts with a number
$_4website = 'not yet';    // valid; starts with an underscore
$täyte = 'mansi';    // valid; 'ä' is (Extended) ASCII 228.
?>

PHP Introduction

PHP Basic Development

PHP Control Structures

PHP Functions

PHP Arrays

PHP File System

Working With PHP Forms

PHP Classes And Objects

Introduction To Database

PHP Cookies

PHP Session

Computer SMS




© w3schoolas.com . All right reserved.www.w3schoolas.com Sitemap