0% found this document useful (0 votes)
20 views

PHP

PHP allows scripts to run on web servers that produce customized responses for each user request based on roles and permissions. Server-side scripts enable hiding source code and require further client requests over the network to show new information. XAMPP is a popular PHP development environment that contains Apache, MariaDB, PHP, and Perl.

Uploaded by

shadow blaze
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)
20 views

PHP

PHP allows scripts to run on web servers that produce customized responses for each user request based on roles and permissions. Server-side scripts enable hiding source code and require further client requests over the network to show new information. XAMPP is a popular PHP development environment that contains Apache, MariaDB, PHP, and Perl.

Uploaded by

shadow blaze
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/ 24

PHP

Server Side Programming


• Scripts on a web server which produce a response
customized for each user's (client's) request to the
website based on the role, access permissions and
the context of the application.
• Server-side scripting enables the website owner to
hide the source code that generates the response to
the user
• client needs to make further requests over the
network to the server in order to show new
information to the user (more round trips)
• Response / request as HTML content over HTTP
protocol
Behind the scenes

WebServer
Server Side Scripts
• Run on a server, embedded in the site’s code (HTML
mostly)
• Designed to interact with backend infrastructure -
Database, Communications and other services
• Facilitates transfer of data from server to browser
(with some filtering, formatting etc)
• Runs on a call
• Powers the dynamic execution of web applications
• Plays a role in how database is designed
• Used to build Application programming Interfaces
XAMPP
• XAMPP is the most popular PHP development
environment
• XAMPP is a completely free, easy to install
Apache distribution containing MariaDB, PHP,
and Perl. The XAMPP open source package has
been set up to be incredibly easy to install and
to use.
Cross(X)platform Apache MariaDB PHP Perl
• Download from
https://www.apachefriends.org/index.html
• <!DOCTYPE html>
<html>
<body>

<?php
echo "My first PHP script!";
?>

</body>
</html>
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)
Output Variables
• The PHP echo statement is often used to output data
to the screen.
• <?php
$txt = “India";
echo "I love $txt!";
?>
• <?php
$x = 5;
$y = 4;
echo $x + $y;
?>
• PHP supports the following data
types:
• String
• Integer
• Float (floating point numbers - also
called double)
• Boolean
• Array
• Object
• NULL
• Resource
<!DOCTYPE html>
<html>
<body>
<?php
$x = 10.365;
var_dump($x);
?>
</body>
</html>
PHP String Functions

• <?php
echo strlen("Hello world!"); // outputs
12
?>
• <?php
echo str_word_count("Hello world!"); //
outputs 2
?>
• <?php
echo strrev("Hello world!"); // outputs
!dlrow olleH
?>
• <?php
echo strpos("Hello
world!", "world"); // outputs 6
?>
• <?php
echo str_replace("world", "Dolly",
"Hello world!"); // outputs Hello
Dolly!
?>
PHP Math

• <?php
echo(pi()); //returns3.1415926535
?>
• <?php
echo(min(0, 150, 30, 20, -8, -200)); //
returns -200
echo(max(0, 150, 30, 20, -8, -200)); //
returns 150
?>
• <?php
echo(abs(-6.7)); // returns 6.7
?>
• <?php
echo(sqrt(64)); // returns 8
?>
• <?php
echo(round(0.60)); // returns 1
echo(round(0.49)); // returns 0
?>
• <?php
echo(rand());
?>
• <?php
echo(rand(10, 100));
?>
• PHP divides the operators in the following
groups:

• Arithmetic operators
• Assignment operators
• Comparison operators
• Increment/Decrement operators
• Logical operators
• String operators
• Array operators
• Conditional assignment operators
Output "Have a good day!" if the current time (HOUR) is less
than 20, and "Have a good night!" otherwise:

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

if ($t < "20") {


echo "Have a good day!";
} else {
echo "Have a good night!";
}
?>
• 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
$x = 1;

do {
echo "The number is: $x <br>";
$x++;
} while ($x <= 5);
?>
• <?php
for ($x = 0; $x <= 10; $x++) {
echo "The number is: $x <br>";
}
?>
PHP Array
• <?php
$cars = array("Volvo", "BMW", "Toyota");
echo count($cars);
?>
• <?php
$age
= array("Peter"=>"35", "Ben"=>"37", "Joe"=>"4
3");
echo "Peter is " . $age['Peter'] . " years
old.";
?>
PHP Form Handling
• <!DOCTYPE HTML>
<html>
<body>

<form action="welcome.php" method="post">


Name: <input type="text" name="name"><br>
E-mail: <input type="text" name="email"><br>
<input type="submit">
</form>

</body>
</html>
PHP DATE
• Here are some characters that are commonly used for
dates:
• d - Represents the day of the month (01 to 31)
• m - Represents a month (01 to 12)
• Y - Represents a year (in four digits)
• l (lowercase 'L') - Represents the day of the week
• <?php
echo "Today is " . date("Y/m/d") . "<br>";
echo "Today is " . date("Y.m.d") . "<br>";
echo "Today is " . date("Y-m-d") . "<br>";
echo "Today is " . date("l");
?>
INCLUDE
• <html>
<body>

<h1>Welcome to my home page!</h1>


<p>Some text.</p>
<p>Some more text.</p>
<?php include 'footer.php';?>

</body>
</html>
• <?php
echo "<p>Copyright &copy; 1999-" . date("Y")
. " W3Schools.com</p>";
?>
PHP Cookie
• What is a Cookie?
• A cookie is often used to identify a user.
• A cookie is a small file that the server
embeds on the user's computer.
• Each time the same computer requests a
page with a browser, it will send the
cookie too.
• With PHP, you can both create and retrieve
cookie values.
PHP Create/Retrieve a Cookie
The following example creates a cookie named "user" with the value "John ". The cookie will expire
after 30 days (86400 * 30). The "/" means that the cookie is available in entire website (otherwise,
select the directory you prefer).We then retrieve the value of the cookie "user" (using the global
variable $_COOKIE). We also use the isset() function to find out if the cookie is set:

• <?php
$cookie_name = "user";
$cookie_value = "John";
setcookie($cookie_name, $cookie_value, time() +
(86400 * 30), "/"); // 86400 = 1 day
?>
<html>
<body>

<?php
if(!isset($_COOKIE[$cookie_name])) {
echo "Cookie named '" . $cookie_name . "' is not set!";
} else {
echo "Cookie '" . $cookie_name . "' is set!<br>";
echo "Value is: " . $_COOKIE[$cookie_name];
}
?>
</body>
</html>

You might also like