PHP Basics


Every PHP application is forged from a small set of basic construction blocks.
From its very inception, PHP was built with the intent of providing simplicity and
choice—and this is clearly reflected in the number of options available in building
applications.

Syntax
PHP’s syntax is derived from many languages—predominantly the C language, but
Perl has also had a lot of influence on its syntax. With the latest object-oriented additions,
more Java-like syntax is creeping in as well. Despite incorporating elements
of so many other languages, PHP’s syntax remains simple and easy to understand.

PHP Tags

Standard Tags
<?php
... code
?>

Short Tags
 <?
... code
?>
<?= $variable ?>
Script Tags

<script language=“php”>
... code
</script>

ASP Tags
 <%
... code
%>

Comments:
PHP gives you multiple choices for your comments:

// Single line comment

# Single line comment

/* Multi-line
comment
*/

/**
* API Documentation Example
*
* @param string $bar
*/
function foo($bar) { }

Both types of single line comments, // and #, can be ended using a newline (\r, \n or
\r\n) or by ending the current PHP block using the PHP closing tag—?>.


Code Block
A code block is simply a series of statements enclosed between two braces:
{
// Some comments
f(); // a function call
}

Language Constructs
Constructs are elements that are built-into the language and, therefore, follow special
rules. Perhaps the most common of them is the echo statement, which allows
you to write data to the script’s output:

echo 10; // will output 10

It’s important to understand that echo is not a function and, as such, it does not have
a return value. If you need to output data through a function, you can use print()
instead:

echo 10;
print (10);

Another very important construct is die(), which is itself an alias of exit(). It allows
you to terminate the script’s output and either output a string or return a numeric
status to the process that called the script.

Share this

0 Comment to " PHP Basics "

Post a Comment