PHP is an open source server-side scripting language designed for Web
development to produce dynamic
Web pages. It is one of the first developed
server-side scripting languages to be embedded into an HTML source document rather than calling an external file to
process data. The code is interpreted by a Web server
with a PHP processor module which generates the resulting Web page. It also has
evolved to include a command-line
interface capability and can be used in standalone graphical
applications. A competitor to Microsoft's Active
Server Pages (ASP) server-side script engine and similar languages, PHP is
installed on more than 20 million Web sites and 1 million Web servers Notable software that uses PHP includes
Definition: The echo () function is used to output the given
argument. It can output all types of data and multiple outputs can be made with
only one echo () command.
Examples:
Echo "Hello";
//Outputs a string
Echo $variable;
//Outputs a variable
Echo "Multiple things " . $on .
" one line";
//Outputs a string, then a variable, then a
string. All are separated with a [.] period
?>
What
You Should Already Know
Before you continue you should have a basic
understanding of the following:
- HTML
- JavaScrip
PHP
Means
- PHP stands for PHP: Hypertext Preprocessor
- PHP is a widely-used, open source scripting language
- PHP scripts are executed on the server
- PHP is free to download and use
PHP
File
- PHP files can contain text, HTML, JavaScript code, and PHP code
- PHP code are executed on the server, and the result is returned to the browser as plain HTML
- PHP files have a default file extension of ".php"
PHP
Can Do
- PHP can generate dynamic page content
- PHP can create, open, read, write, and close files on the server
- PHP can collect form data
- PHP can send and receive cookies
- PHP can add, delete, modify data in your database
- PHP can restrict users to access some pages on your website
- PHP can encrypt data
With PHP you are not limited to output HTML. You
can output images, PDF files, and even Flash movies. You can also output any
text, such as XHTML and XML
PHP
Uses
- PHP runs on different platforms (Windows, Linux, Unix, Mac OS X, etc.)
- PHP is compatible with almost all servers used today (Apache, IIS, etc.)
- PHP has support for a wide range of databases
XML Tools
Integrated suite of tools ideal for:
|
PHP Syntax
A PHP script always
starts with and ends with ?>. A PHP script can be placed anywhere
in the document.
On servers with
shorthand-support, you can start a PHP script with .
For maximum
compatibility, we recommend that you use the standard form (
// 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 script that sends the text "Hello World!"
back to the browser:
Example
echo "Hello World!";
?>
Algebra x=5, y=6, z=x+y
These letters are called variables, and variables can be used to hold values (x=5) or
expressions (z=x+y).
PHP Variables
A variable can have a short name, like x, or a more
descriptive name, like carName.
Rules for PHP variable
- Variables in PHP starts with a $ sign, followed by the name of the variable
- The variable name must begin with a letter or the underscore character
- A variable name can only contain alpha-numeric characters and underscores (A-z, 0-9, and _ )
- A variable name should not contain spaces
- Variable names are case sensitive (y and Y are two different variables)
Creating (Declaring) PHP
PHP has no command for declaring a variable.
A variable is created the moment you first assign a
value to it:
$myCar="Volvo";
After the execution of the statement above, the
variable myCar will
hold the value Volvo.
Tip: If
you want to create a variable without assigning it a value, then you assign it
the value ofnull.
Let's create a variable containing a string, and a
variable containing a number:
$txt="Hello World!";
$x=16;
?>
$x=16;
?>
PHP
is a Loosely Typed Language
In PHP, a variable does not need to be declared
before adding a value to it.
In the example above, notice that we did not have
to tell PHP which data type the variable is.
PHP automatically converts the variable to the
correct data type, depending on its value.
In a strongly typed programming language, you have
to declare (define) the type and name of the variable before using it.
PHP
Variable Scope
- local
- global
- static
- parameter
Local
Scope
A variable declared within a
PHP function is local and can only be accessed within that function. (the
variable has local scope):
$a = 5; // global scope
function myTest()
{
echo $a; // local scope
}
myTest();
?>
function myTest()
{
echo $a; // local scope
}
myTest();
?>
The script above will not produce any output
because the echo statement refers to the local scope variable $a, which has not
been assigned a value within this scope.
You can have local variables with the same name in
different functions, because local variables are only recognized by the
function in which they are declared.
Local variables are deleted as soon as the function
is completed.
Global
Scope
Global scope refers to any variable that is defined
outside of any function.
Global variables can be accessed from any part of
the script that is not inside a function.
To access a global variable from within a function,
use the global keyword:
$a = 5;
$b = 10;
function myTest()
{
global $a, $b;
$b = $a + $b;
}
myTest();
echo $b;
?>
$b = 10;
function myTest()
{
global $a, $b;
$b = $a + $b;
}
myTest();
echo $b;
?>
The script above will output 15.
PHP also stores all global variables in an array
called $GLOBALS[index]. Its index is the name of the variable. This
array is also accessible from within functions and can be used to update global
variables directly.
The example above can be rewritten as this:
$a = 5;
$b = 10;
function myTest()
{
$GLOBALS['b'] = $GLOBALS['a'] + $GLOBALS['b'];
}
myTest();
echo $b;
?>
$b = 10;
function myTest()
{
$GLOBALS['b'] = $GLOBALS['a'] + $GLOBALS['b'];
}
myTest();
echo $b;
?>
Setatic
Scop
When a function is completed, all of its variables are
normally deleted. However, sometimes you want a local variable to not be
deleted.
To do this, use the static keyword
when you first declare the variable:
static $rememberMe;
Then, each time the function is called, that
variable will still have the information it contained from the last time the
function was called.
Parameters
parameter is a local variable whose value is
passed to the function by the calling code.
Parameters are declared in a parameter list as part
of the function declaration:
function myTest($para1,$para2,...)
{
// function code
}
{
// function code
}
Parameters are alAso called arguments. We will
discuss them in more detail when we talk about functions.