0% found this document useful (0 votes)
54 views37 pages

Lecture - 9 - PHP - Overview

PHP is a server-side scripting language used for developing dynamic web applications. It was originally created in 1995 and combines elements of C, Java, and Perl. PHP code is embedded into HTML and processed by a PHP interpreter on the server to create the final HTML code that is sent to the browser. Some key advantages of PHP include being freely available, supporting many databases and operating systems, and having a similar syntax to languages like C and Perl. Popular websites built with PHP include Facebook, Wikipedia, and Yahoo. The basics of PHP include variables, data types, operators, conditional statements, arrays, loops, and functions. PHP can also interact with HTML forms to collect and process user input data.

Uploaded by

splokbov
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
54 views37 pages

Lecture - 9 - PHP - Overview

PHP is a server-side scripting language used for developing dynamic web applications. It was originally created in 1995 and combines elements of C, Java, and Perl. PHP code is embedded into HTML and processed by a PHP interpreter on the server to create the final HTML code that is sent to the browser. Some key advantages of PHP include being freely available, supporting many databases and operating systems, and having a similar syntax to languages like C and Perl. Popular websites built with PHP include Facebook, Wikipedia, and Yahoo. The basics of PHP include variables, data types, operators, conditional statements, arrays, loops, and functions. PHP can also interact with HTML forms to collect and process user input data.

Uploaded by

splokbov
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 37

PHP overview

Content

 Introduction to PHP

 History and development

 Basics of PHP programming


What is PHP ?
 PHP = Hypertext preprocessor
 Server side scripting language
 Used for development of dynamical webpages
 Part of typical LAMP combination
 Linux, Apache, MySQL and PHP
 Much of its syntax is borrowed from C, Java and Perl with
a couple of unique PHP-specific features.
 Open source software – free to download and use
How it works
 PHP code is usually embedded into HTML

 Processing the code :


1) The HTML code stands as it is

2) The PHP scripts are executed to create final HTML code

3) Both parts are combined and back

4) Resulting HTML is interpreted by a browser


Advantages of PHP

 Freely available
 The PHP group provides complete source code free of
charge
 Similar syntax to C, Pearl
 Works with many operating systems
 Can be deployed on many web servers
 Interacts with lots of databases
 It is supported by many providers of webhosting
Websites using PHP

 More than 20 million Internet domains are hosted on


servers with PHP installed

 Significant examles
 User-facing portion of Facebook
 Wikipedia (MediaWiki)
 Yahoo!
 MyYearbook
What do You Need to work with PHP?

 WAMP (Windows Apache MySQL PHP)software bundle


 Easy PHP version 5.3..
 It contains the web server – Apache and a database – MySQL
 The web server can be accessed through localhost –
http://localhost/or http://127.0.0.1
Port Number
 By default, apache uses port 80
 If another web server, e.g. IIS is also installed, both cannot
be used simultaneously
 either the web server must be stopped when apache is being
used
 or if the 2 servers are to run simultaneously, they should use
different port numbers. E.g. Apache can use port 8086.
Accessed by specifying port number -
http://localhost:8086/ or http://127.0.0.1:8086/
What is a PHP file?
 A PHP file
 is saved as either .php (recommended)
 or .php3 or php4
 It can contain text, HTML tags and PHP
 scripts
 Only HTML code is sent to the browser
 Where to Save a PhP file
 All files must be placed either in the directory "www" or an
alias that has been created, so that PHP can interpret the PHP
pages.
 To view the PHP pages, select "local Web” or an Alias on the
"Administration" page of EasyPHP.
Basics of syntax
 Scripting block starts with <?php and ends with ?>

 Each code line in PHP must ends with a (;)

 Comments
 // ,# comment
 /* comment */
 Writing of the plain text
 Echo “text”
 print “text”
Example 1- Helloworld.php
<html>
<head>
<title>My First PHP Page</title>
</head>
<body>
<?php
echo "Hello World!";
?>
</body>
</html>
Variables in PHP
 Each variable starts with $ symbol

 Variable name can contain only a-Z,0-9,_

 Are case sensitive and CANNOT contain spaces

<?php
$txt = "HelloWorld!";
$number = 16;
?>
Variable types

 Numerical
 Integer – positive as well as negative, including 0
 Float – real numbers, 14 digits accuracy

 Logical
 Boolean - True x False, not case sensitive

 Alphabetical
 String – set of characters
Working with variables
 Settype($var, “integer”)
 allows you to set variable according to your wish
 Gettype()
 write the type of variable

 (.)
 Connects 2 variables of string type
 strlen()
 finds the length of a string
Example 2
<?php
$var = "Bob";
$Var = "Joe";
echo $var ;//outputs Bob
echo $Var;//outputs Joe
$4site = "not yet";// invalid; starts with a number
$_4site = "not yet";// valid; starts with an underscore
?>
Example 3 – String variables

<?php <?php
$txt="Hello World"; $txt1="Hello World.";
echo $txt; ?> $txt2=“We are in 2008.";
echo $txt1 . " " . $txt2;
/*note use of ‘.’ for
concatenation*/
?>

Investigate the strlen() and strpos() functions


PHP Operators
 Uses standard mathematical operators +,-,/,*
 Special symbol ++ (--) for increase (decrease) by 1

 Comparison operators >,<, >=


 Special cases
== is equal
!= is different

 Assignment operators
 x+=y  x=x+y
Logical operators

 && = and
 || = or
 At least one of condition is fulfilled
 ! = not
 xor
 Exactly one statement is evaluated as true
Conditional Statements
 If/ else
 After each statement stands (;)
 If more than one command should be executed, use curly braces { }

 Switch / break
 Used for choosing one possibility from multiple cases
Switch ($var )
{
case : “x” : echo “good”;
break;
default : echo “wrong input” ;
}
Example 4
<html><body>
<?php
switch ($examGrade)
{ case “A”: echo “ Very Good";
break;
case “F”: echo “ Failure";
break;
default: echo “ Average";
break;
}
?></body></html>
Arrays in PHP

 Numeric array
 Each element of array has its ID number (first 0!!)
 $names = array(“Petr”, “Joe”);
 $names[0] = "Petr";

 Associative Arrays
 Each element is assigned its value
 $ages = array("Peter"=>32, "Joe"=>34);
 $ages['Peter'] = "32";
PHP Looping
 while
 loops repeat until final condition is reached
$i =1;
while ($i<=10)
{
echo $i;
$i++;
}
 do...while
 kind of reversed while function
Do { code to be executed;}
While(final condition);
PHP Looping
 for
 Repeats the specific part of code so many times we choose

for ($i=1; $i<=10; $i++)

Initial condition final condition running decsription


Example 6
<?php
/* printing an array */
$modules[0]=“Software Engineering”;
$modules[1]=“Database Systems”;
$modules[2]=“Web Technologies”;

for ($i=0;$i<count($modules);$i++)
{
echo (“module $i is modules[$i] <br/>”);
}
?>
Example 7
<?php
/* printing an associative array */
$modules[CSE 1041]=“ Web Technologies”;
$modules[CSE 2242]=“Computer Graphics”;
$modules[CSE 2046]=“Multimedia Authoring”;

foreach ($moduel as $key=>$value)


// foreach loop is used to loop through arrays
{
echo ("$key is the module: $value<br/>“);
} ?>
HTML inside PHP
 If inside quotes, the Html tags are returned as a text by PHP
module
 Treated as a HTML tag by

<?php
echo
"<TR>
<TD>".$i."</TD><TD>".$i*$i."</TD></TR>\n";
?>
PHP Functions

 All function starts with function($parameter)


 Requirements for naming functions are same as these for
variables
 The { mark opens the function code, while } mark closes it
 It can have either defined or no parameter

 More than 700 built-in functions available


Example 8
<html><body>
<?php
//creating the function printHelloWorld
function printHelloWorld(){
echo “Hello World”;
}
//calling the function printHelloWorld
Print HelloWorld();
?>
</body></html>
Example 9 - Functions with
parameters
<html><body>
<?php
function sayHi($fname) {
echo $fname . "<br />";
}
echo "Hi";
sayHi("John");
echo "Hi";
sayHi("Jane");
</body></html>
Example 10 - Functions which
return a value
<html> <body>
<?php
function sum($num1,$num2) {
$total = $num1 + $num2;
//returning a value
return $total; }
echo "1 + 2 = ".sum(1,2);
?> </body> </html>
PHP Forms and User Input
 Used to gain information from users by means of HTML
 Information is worked up by PHP
<html>
<body>
<form action="welcome.php" method="post">
Name: <input type="text" name="name" />
Age: <input type="text" name="age" />
<input type="submit" />
</form>
</body>
</html>
The $_GET Variable
 Used to collect values from a form
 Displays variable names and values are in the URL
 http://www.w3schools.com/welcome.php?name=jo&age=39
 Can send limited amount of information (max. 100
characters)
<html>
<body>
Welcome <?php echo $_GET["name"]; ?> <br />
You are <?php echo $_GET["age"]; ?> years old
</body>
</html>
The $_POST variable
 Used to collect values from a form
 Information from a form is invisible
 http://www.w3schools.com/welcome.php
 No limits on the amount of information to be send

<html>
<body>
Welcome <?php echo $_POST["name"]; ?><br />
You are <?php echo $_POST["age"]; ?> years old.
</body>
</html>
Exercise
Write the HTML code for FormInput.htm having a layout as in
the diagram below:
txt_id
txt_pwd
txt_email
txt_module
rb_classSize
cb_delivery
cb_lab

txt_others

Method POST,
processForm.php
Exercise contd
Write the PHP code for the processForm.php to get the display as
in the diagram below, based on the input form FormInput.htm
in the previous slide
Including other files
<html> <body>
<?php include(“myPage.php"); ?>
<h1>Web Technologies Course</h1>

</body>
</html>

Exercise : Write your website so that it includes your


website header and footer from a separate php file.
Sources
 http://www.w3schools.com/PHP/
 http://en.wikipedia.org/wiki/PHP
 http://cz.php.net/
 http://www.linuxsoft.cz/
 Previous web tech 1041 lecture slides

You might also like