Krishna Priya April 19, 2011 C-DAC, Hyderabad
Krishna Priya April 19, 2011 C-DAC, Hyderabad
Krishna Priya April 19, 2011 C-DAC, Hyderabad
Krishna priya
April 19, 2011
C-DAC, Hyderabad
What is PHP?
PHP == ‘Hypertext Preprocessor’
Open-source, server-side scripting
language
Used to generate dynamic web-pages
PHP scripts reside between reserved PHP
tags
This allows the programmer to embed PHP
scripts within HTML pages
What is PHP (cont’d)
Interpreted language, scripts are parsed at run-
time rather than compiled beforehand
Executed on the server-side
Source-code not visible by client
‘View Source’ in browsers does not display the PHP code
Various built-in functions allow for fast
development
Compatible with many popular databases
including MySQL, PostgreSQL, Oracle, Sybase,
Informix, and Microsoft SQL Server.
Brief History of PHP
PHP (PHP: Hypertext Preprocessor) was created
by Rasmus Lerdorf in 1994. It was initially
developed for HTTP usage logging and server-
side form generation in Unix.
No structure type:
There is no struct in PHP
No pointers:
There are no pointers available in PHP.
PHP does support variable references. You can
also emulate function pointers to some extent,
in that function names can be stored in
variables and called by using the variable
rather than a literal name.
Differences compare to C
No prototypes:
Functions do not need to be declared before
their implementation is defined, as long as the
function definition can be found somewhere in
the current code file or included files.
PHP Language Basics
Structurally similar to C/C++
All PHP statements end with a semi-colon
Each PHP script must be enclosed in the reserved
PHP tag
PHP Tag Styles as follows:
<?php
…
short-open tag ASP-style tags
?> <?...?> <%...%>
/* C-style comments
These can span multiple lines */
PHP Variables
PHP variables must begin with a “$” sign
Case-sensitive ($Foo != $foo != $fOo)
Certain variable names reserved by PHP
For Example Form variables ($_POST,
$_GET) Etc.
<?php
$foo = 25; // Numerical variable
$bar = “Hello”; // String variable
Notice how echo ‘5x5=$foo’ outputs $foo rather than replacing it with 25
Strings in single quotes (‘ ’) are not interpreted or evaluated by PHP
Concatenation
Use a period to join strings into one.
<?php
$string1=“Hello”;
$string2=“PHP”; Hello PHP
$string3=$string1 . “ ” . $string2;
echo $string3;
?>
<?php
$heading=“\”Computer Science\””;
“Computer Science”
Print $heading;
?>
Operators Examples
Assume variable A holds 10 and variable B holds 20 then:
No THEN in PHP
PHP Loop Types
Loops in PHP are used to execute the same block of code a
specified number of times. PHP supports following four loop
types.
While while - loops through a block of code if and as
long as a specified condition is true.
do...while do...while - loops through a block of code once,
and then repeats the loop as long as a special
condition is true.
$datedisplay=date(“l, F m, Y”);
Tuesday, April 19, 2011 echo $datedisplay;
$date display=date(“D”);
Tue
echo $d
Functions
PHP functions are similar to other programming languages. A
function is a piece of code which takes one more input in the form
of parameter and does some processing and returns a value.
There are two parts which should be clear to you:
Creating a PHP Function
Calling a PHP Function
function functionName()
{
code to be executed;
}
PHP File Inclusion
You can include the content of a PHP file into another PHP
file before the server executes it. There are two PHP
functions which can be used to included one PHP file into
another PHP file.