0% found this document useful (0 votes)
436 views60 pages

Unit - 3 PHP - 1

The document discusses PHP concepts including: 1. It provides an introduction to PHP including its history, uses, and advantages such as being free, running on many platforms, and being well-suited for web development. 2. It covers PHP syntax, how to run PHP code, and basic PHP concepts like variables, data types, operators, and functions. 3. It discusses how PHP can be used to dynamically generate web page content, work with databases, handle forms, and more server-side tasks.

Uploaded by

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

Unit - 3 PHP - 1

The document discusses PHP concepts including: 1. It provides an introduction to PHP including its history, uses, and advantages such as being free, running on many platforms, and being well-suited for web development. 2. It covers PHP syntax, how to run PHP code, and basic PHP concepts like variables, data types, operators, and functions. 3. It discusses how PHP can be used to dynamically generate web page content, work with databases, handle forms, and more server-side tasks.

Uploaded by

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

K Mahesh

RCEW, Pasupula (V), Nandikotkur Road,


Near Venkayapalli, KURNOOL
Syllabi
• Introduction to PHP: The problem with other
Technologies ( Servelets and JSP),
• Downloading, installing, configuring PHP,
Programming in a Web environment and the
anatomy of a PHP Page.
• Overview of PHP Data types and Concepts: Variables
and data types, Operators, Expressions and
Statements, Strings, Arrays and Functions.
• PHP Advanced Concepts: Using Cookies, Using HTTP
Headers, Using Sessions, Authenticating users, Using
Environment and Configuration variables, Working
with Date and Time.
Server Side Programming

• ASP
• ASP.NET
• PHP
• JSP
• Servlet

RCEW, Pasupula (V), Nandikotkur Road,


Near Venkayapalli, KURNOOL
Disadvantages of Servlet
• Servlet is a mixture of Java skills and web related HTML skills,
because you have to write the business logic in Java and the
presentation you should use the HTML, so the role based
development is missing in pure Servlet. The developer who is
writing Servlet should know Java and HTML.
• If we used Servlet technology in our application, it is very
difficult for enhancement and bug fixing.
• The servlet technology require more steps to develop,
Servlet require too long timefor development.
• Designing in Servlet is difficult and slow down the
application.
• You need to a Java Runtime environment(JRE) on the server
to run Servlet.
Disadvantages of JSP
• JSP pages require about double the disk space to hold the
page, Because JSP pages are translated into class files, the
server has to store the resultant class file with the JSP pages.
• JSP pages must be compiled on the server when first
accessed this initial compilation produces a noticeable delay
when accessing the JSP page for the first time.
• JSP implementation typically issue poor diagnostics, Because
JSP pages are translated, and then compiled into Java
servlets, errors that creep in your pages are rarely seen as
errors arising from the coding of JSP pages.
• Lack of separation between presentation and logic that
means providing multiple presentations carries a very high
cost
PHP Introduction

•Created by Rasmus Lerdorf way back in 1994.


•PHP started out as a set of simple tools coded in the C
language to replace the Perl scripts that Rasmus was
using on his personal homepage (hence the original
meaning of the ―PHP ― acronym).
•He released PHP to the general public in 1995, and
called it PHP version 2.
•PHP is a recursive acronym for “PHP: Hypertext
Preprocessor” -- It is a widely-used open source general-
purpose scripting language that is especially suited for
web development and can be embedded into HTML.
PHP Introduction
•> PHP is a server-side scripting language
•> PHP scripts are executed on the server
•> PHP supports many databases (MySQL, Informix,
Oracle, Sybase, Solid, PostgreSQL, Generic ODBC, etc.)
•> PHP is open source software
•> PHP is free to download and use
• PHP files can contain text, HTML, CSS, JavaScript, and
PHP code
• PHP code is executed on the server, and the result is
returned to the browser as plain HTML
• PHP files have extension ".php"
PHP Introduction
•> PHP runs on different platforms (Windows,
Linux, Unix, etc.)
•> PHP is compatible with almost all servers
used today (Apache, IIS, etc.)
•> PHP is FREE to download from the official PHP
resource: www.php.net
•> PHP is easy to learn and runs efficiently on
the server side
What can do PHP?
• PHP can generate dynamic page content
• PHP can create, open, read, write, delete, 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 be used to control user-access
• PHP can encrypt data
Why PHP?
• PHP runs on various platforms (Windows, Linux,
Unix, Mac OS X, etc.)
• PHP is compatible with almost all servers used
today (Apache, IIS, etc.)
• PHP supports a wide range of databases
• PHP is free. Download it from the official PHP
resource: www.php.net
• PHP is easy to learn and runs efficiently on the
server side
• PHP 7 is much faster than the previous popular
stable release (PHP 5.6)
PHP Installation
• To start using PHP
– Find a web host with PHP and MySQL support
– Install a web server on your own PC, and then install
PHP and MySQL
• Set Up PHP on Your Own PC
– install a web server
– install PHP
– install a database, such as MySQL
• PHP Online Compiler / Editor
MySQL
•> MySQL is a database server
•> MySQL is ideal for both small and large
applications
•> MySQL supports standard SQL
•> MySQL compiles on a number of platforms
•> MySQL is free to download and use
PHP Syntax
<?php
// PHP code goes here
?>

• A PHP script can be placed anywhere in the document.


• A PHP script starts with <?php and ends with ?>
• A PHP file normally contains HTML tags, and some PHP
scripting code.
• Built-in PHP function "echo" to output the text.
• In PHP, keywords (e.g. if, else, while, echo, etc.),
classes, functions, and user-defined functions are not
case-sensitive.
• variable names are case-sensitive.
PHP – How to run
<!DOCTYPE html>
<html>
<body>

<?php
echo  "Hello World";
?>

</body>
</html>
•Install XAMPP
•Start Apache & MySQL services
•Save the file as .php
•Copy file.php to htdocs (C:/XAMPP/htdocs)
•Open browser with  http://localhost/file.php
Client – Server - Client
Client – Server - Client

Render as HTML
PHP Comments

<!DOCTYPE html>
<html>
•In PHP, we use <body>

<?php
// single-line comment // This is a single-line comment

# This is also a single-line comment


# This is also a single-line /*
comment This is a multiple-lines comment block
that spans over multiple
lines
*/
/* */ Multi line comment. ?>

</body>
</html>
PHP Variables
•> Variables are used for storing values, like text strings, numbers or arrays.
•> When a variable is declared, it can be used over and over again in your
script.
•> All variables in PHP start with a $ sign symbol.
• > In PHP, a variable does not need to be declared before adding a value to
it.
•> PHP automatically converts the variable to the correct data type,
depending on its value.

<?php
$txt = "Hello world!";
$x = 5;
$y = 10.5;
?>
PHP Variables
• A variable starts with the $ sign, followed by the name of the variable
• A variable name must start with a letter or the underscore character
• A variable name cannot start with a number
• A variable name can only contain alpha-numeric characters and
underscores (A-z, 0-9, and _ )
• Variable names are case-sensitive ($age and $AGE are two different
variables)
• PHP is a Loosely Typed Language

<?php
$txt = “RECW";
echo "I love $txt!";
?>
PHP Concatenation
•> The concatenation operator (.) is used to put
two string values together.
•> To concatenate two string variables together,
use the concatenation operator:

<?php
$txt = "RECW";
echo "I love" . $txt . "!";
?>
Variables Scope
• In PHP, variables can be declared anywhere in the
script.
• The scope of a variable is the part of the script
where the variable can be referenced/used.
• PHP has three different variable scopes:
– local
– global
– static
Local Scope
<!DOCTYPE html>
<html>
<body>

<?php
function myTest() {
$x = 5; // local scope
echo "<p>Variable x inside function is: $x</p>";
}
myTest();

// using x outside the function will generate an error


echo "<p>Variable x outside function is: $x</p>";
?>

</body>
</html>
Global Scope
<!DOCTYPE html>
<html>
<body>

<?php
$x = 5; // global scope

function myTest() {
// using x inside this function will generate an error
echo "<p>Variable x inside function is: $x</p>";
}
myTest();

echo "<p>Variable x outside function is: $x</p>";


?>

</body>
</html>
Global Scope
The global keyword is used to access a global variable from within a function.

<!DOCTYPE html>
<html>
<body>

<?php
function myTest() {
$x = 5; // local scope
echo "<p>Variable x inside function is: $x</p>";
}
myTest();

// using x outside the function will generate an error


echo "<p>Variable x outside function is: $x</p>";
?>

</body>
</html>
Global Scope
PHP also stores all global variables in an array called $GLOBALS[index].
The index holds the name of the variable.
This array is also accessible from within functions and can be used to update global variables
directly.

<!DOCTYPE html>
<html>
<body>

<?php
$x = 5;
$y = 10;

function myTest() {
$GLOBALS['y'] = $GLOBALS['x'] + $GLOBALS['y'];
}

myTest();
echo $y;
?>
</body>
</html>
Static Scope
Normally, when a function is completed/executed, all of its variables are deleted.
However, sometimes we want a local variable NOT to be deleted. We need it for a further job.
To do this, use the static keyword when you first declare the variable:
<!DOCTYPE html>
<html>
<body>

<?php
function myTest() {
static $x = 0;
echo $x;
$x++;
}
OUT PUT
myTest();
0
echo "<br>";
myTest(); 1
echo "<br>"; 2
myTest();
?>

</body>
</html>
echo and print Statements
• two basic ways to get output: echo and print.
• echo and print are more or less the same.
• echo has no return value while print has a return value of 1
so it can be used in expressions. 
• echo can take multiple parameters (although such usage is
rare) while print can take one argument. 
• echo is marginally faster than print.
• The print statement can be used with or without
parentheses: print or print().
<?php
print "<h2>PHP is Fun!</h2>";
print "Hello world!<br>";
print "I'm about to learn PHP!";
?>
PHP Data Types
Variables can store data of different types, and different data
types can do different things.
PHP supports the following data types:
• String
• Integer
• Float (floating point numbers - also called double)
• Boolean
• Array
• Object
• NULL
• Resource (reference to functions and resources external to
PHP)
PHP String
• A string can be any text inside quotes. You can use
single or double quotes

<!DOCTYPE html>
<html>
<body>

<?php
$x = "Hello world!";
$y = 'Hello world!';

echo $x;
echo "<br>";
echo $y;
?>

</body>
</html>
PHP String Functions
• strlen() - Return the Length of a String
• str_word_count() - Count Words in a String
• strrev() - Reverse a String
• strpos() - Search For a Text Within a String
• str_replace() - Replace Text Within a String
<!DOCTYPE html>
<html>
<body>

<?php
echo strlen("Hello world!")."<br>";
echo str_word_count("Hello world!")."<br>";
echo strrev("Hello world!")."<br>";
echo strpos("Hello world!","world")."<br>";
echo str_replace("world","RECW","Hello world!");
?>

</body>
</html>
PHP Integer
• An integer data type is a non-decimal number between
-2,147,483,648 and 2,147,483,647.
• An integer must have at least one digit
• An integer must not have a decimal point
• An integer can be either positive or negative
• Integers can be specified in: decimal (base 10), hexadecimal (base
16), octal (base 8), or binary (base 2) notation
• The PHP var_dump() function returns the data type and value:
<!DOCTYPE html>
<html>
<body>
<?php
$x = 5985;
OUTPUT
var_dump($x);
int(5985)
?>
</body>
</html>
PHP Float
• A float (floating point number) is a number with a decimal point or
a number in exponential form.

<!DOCTYPE html>
<html>
<body>

<?php
$x = 10.365;
var_dump($x);
?>

</body>
</html>

OUTPUT
float(10.365)
PHP Boolean
• A Boolean represents two possible states: TRUE or FALSE.

$x = true;
$y = false;
PHP Array
• An array stores multiple values in one single variable.

<!DOCTYPE html>
<html>
<body>

<?php
$cars = array("Volvo","BMW","Toyota");
var_dump($cars);
?>

</body>
</html>

OUTPUT
array(3) { [0]=> string(5) "Volvo" [1]=> string(3) "BMW" [2]=> string(6) "Toyota" }
PHP Object
• Classes and objects are the two main aspects of object-oriented
programming.
• A class is a template for objects, and an object is an instance of a
class.
• When the individual objects are created, they inherit all the
properties and behaviors from the class, but each object will have
different values for the properties.
<!DOCTYPE html>
<html>
<body>

<?php
class Car {
public $color;
public $model;
public function __construct($color, $model) {
$this->color = $color;
$this->model = $model;
}
public function message() {
return "My car is a " . $this->color . " " . $this->model . "!";
}
}

$myCar = new Car("black", "Volvo");


echo $myCar -> message();
echo "<br>";
$myCar = new Car("red", "Toyota");
echo $myCar -> message();
?>
OUTPUT
</body> My car is a black Volvo!
</html> My car is a red Toyota!
PHP NULL Value
• Null is a special data type which can have only one value: NULL.
• A variable of data type NULL is a variable that has no value
assigned to it.
• Variables can also be emptied by setting the value to NULL:

<!DOCTYPE html>
<html>
<body>

<?php
$x = "Hello world!";
$x = null;
var_dump($x);
?>

</body> OUTPUT
</html> NULL
PHP Operators

Operators are used to operate on values.


• Arithmetic operators
• Assignment operators
• Comparison operators
• Increment/Decrement operators
• Logical operators
• String operators
• Array operators
• Conditional assignment operators
PHP Arithmetic Operators
PHP Assignment Operators
PHP Comparison Operators
PHP Increment / Decrement Operators

<!DOCTYPE html>
<html>
<body>

<?php
$x = 10;
echo ++$x;
?>

</body>
</html>
PHP Logical Operators

<!DOCTYPE html>
<html>
<body>

<?php
$x = 100;
$y = 50;

if ($x == 100 and $y == 50) {


echo "Hello world!";
}
?>

</body>
</html>
PHP String Operators

<!DOCTYPE html>
<html>
<body>

<?php
$txt1 = "Hello";
$txt2 = " world!";
$txt1 .= $txt2;
echo $txt1;
?>

</body>
</html>
PHP Array Operators
Conditional Assignment Operators

<!DOCTYPE html>
<html>
<body>

<?php
$x=100;
$y=20;
echo $st=($x>$y) ? "$x is greater" : "$y is greater";
?>

</body>
</html>
PHP Conditional Statements
•> if statement - use this statement to execute
some code only if a specified condition is true
•> if...else statement - use this statement to
execute some code if a condition is true and
another code if the condition is false
•> if...elseif....else statement - use this statement to
select one of several blocks of code to be executed
•> switch statement - use this statement to select
one of many blocks of code to be executed
PHP - The if Statement

if (condition) {
  code to be executed if condition is true;
}

<!DOCTYPE html>
<html>
<body>

<?php
$t = date("H");

if ($t < "20") {


echo "Have a good day!";
}
?>

</body>
</html>
PHP - The if...else Statement

if (condition) {
  code to be executed if condition is true;
} else {
  code to be executed if condition is false;
}
The if...elseif...else Statement

if (condition) {
  code to be executed if this condition is true;
} elseif (condition) {
  code to be executed if first condition is false
and this condition is true;
} else {
  code to be executed if all conditions are false;
}
PHP switch Statement
switch (n) {
  case label1:
    code to be executed if n=label1;
    break;
  case label2:
    code to be executed if n=label2;
    break;
  case label3:
    code to be executed if n=label3;
    break;
    ...
  default:
    code to be executed if n is different from all labels;
}
<!DOCTYPE html>
<html>
<body>

<?php
$favcolor = "red";

switch ($favcolor) {
case "red":
echo "Your favorite color is red!";
break;
case "blue":
echo "Your favorite color is blue!";
break;
case "green":
echo "Your favorite color is green!";
break;
default:
echo "Your favorite color is neither red, blue, nor green!";
}
?>

</body>
</html>
PHP Loops
In PHP, we have the following loop types:
•while - loops through a block of code as long as the specified
condition is true

•do...while - loops through a block of code once, and then repeats


the loop as long as the specified condition is true

•for - loops through a block of code a specified number of times

•foreach - loops through a block of code for each element in an array


PHP while Loop
while (condition is true) {
  code to be executed;
}
<!DOCTYPE html>
<html>
<body>

<?php
$x = 1;

while($x <= 5) {
echo "The number is: $x <br>";
$x++;
}
?>

</body>
</html>
PHP do while Loop
do {
  code to be executed;
} while (condition is true);

<!DOCTYPE html>
<html>
<body>

<?php
$x = 1;

do {
echo "The number is: $x <br>";
$x++;
} while ($x <= 5);
?>

</body>
</html>
PHP for Loop
for (init counter; test counter; increment counter) {
  code to be executed for each iteration;
}

<!DOCTYPE html>
<html>
<body>

<?php
for ($x = 0; $x <= 10; $x++) {
echo "The number is: $x <br>";
}
?>

</body>
</html>
PHP foreach Loop
The foreach loop works only on arrays, and is used to loop through each
key/value pair in an array.

foreach ($array as $value) {
  code to be executed;
}
<!DOCTYPE html>
<html>
<body>

<?php
$colors = array("red", "green", "blue", "yellow");

foreach ($colors as $value) {


echo "$value <br>";
}
?>

</body>
</html>

You might also like