0% found this document useful (0 votes)
61 views41 pages

Unit-4 PHP

PHP is a server-side scripting language used for web development. It allows developers to add dynamic and interactive elements to web pages. Some key points about PHP: - It is open source and runs on many platforms like Windows and Linux. - PHP code is embedded into HTML and executed on the server to create dynamic web pages. It can interface with databases, emails, and other services. - Features include simplicity, portability, speed, and being extensible. It has basic syntax like variables, operators, conditional and loop statements. - XAMPP is a free and open source tool that provides a local testing environment for PHP, MySQL, Apache, and Perl. It allows testing PHP

Uploaded by

varsheni
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)
61 views41 pages

Unit-4 PHP

PHP is a server-side scripting language used for web development. It allows developers to add dynamic and interactive elements to web pages. Some key points about PHP: - It is open source and runs on many platforms like Windows and Linux. - PHP code is embedded into HTML and executed on the server to create dynamic web pages. It can interface with databases, emails, and other services. - Features include simplicity, portability, speed, and being extensible. It has basic syntax like variables, operators, conditional and loop statements. - XAMPP is a free and open source tool that provides a local testing environment for PHP, MySQL, Apache, and Perl. It allows testing PHP

Uploaded by

varsheni
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/ 41

UNIT-4

PHP
What is PHP?
• Server-side scripting language.
• PHP stands for "PHP Hypertext Preprocessor“
• PHP is a widely-used, open source.
• PHP scripts are executed on the server
• Used to make web pages dynamic:
• interface with other services: database, e-mail, etc
• PHP code can be embedded in HTML5 code
PHP history
• Created in 1994 by Rasmus Lerdorf,
• Originally called “Personal Home Page” language.
• He did not intend PHP to be come a new programming
language.
• He open sourced it and it grew on its own.
• Graduated  University of Waterloo in Applied Science in
System Engineering
Features of PHP
• Simplicity
• Clearly written manual
• Consistent and logical syntax
• It may take only two hours.
• It has Kiss principle for (Keep it simple stupid).
•Portability
•work on different platforms
•Speed
•run faster that most scripting languages
Features of PHP
• Open source
• its source code is freely available on the web
•Extensible
•it can support new technology and add new methods
PHP Basic Syntax
• A PHP script can be placed anywhere in the document.
• A PHP script starts with
<?php and ends with ?>:
XAMPP SERVER
• XAMPP is an open-source web server solution package. It is mainly
used for web application testing on a local host webserver.
• XAMPP stands for:
• X = Cross-platform
• A = Apache Server
• M = MariaDB
• P = PHP
• P = Perl
Why Do You Need XAMPP?
• To run any PHP program, you will need Apache or MYSQL databases,
both supported by XAMPP.

• XAMPP helps you to run your program smoothly.


Steps
• 1. Go to “C:\xampp\htdocs” and inside it, create a folder. Let’s call it
“demo”. It’s considered good practice to create a new folder for
every project you work on.
• 2. Inside the demo folder, create a new text file and name it “index.php”
and write the following script
• 3. Now, to see the script output, open the XAMPP control panel and
start Apache to host the local webserver, where our script will be
running.
• 4. Now navigate to your browser and type in “localhost/demo/”
in the address bar to view the output.
Example
echo and print Statements
• In PHP there are two basic ways to get output: echo and print.
• echo can take multiple parameters (although such usage is rare)
while print can take one argument.

• The echo statement can be used with or without parentheses:


echo or echo()
The print statement can be used with or without parentheses: print or
print().
Comments in PHP
• A comment in PHP code is a line that is not read/executed as part of the
program
Data Types
• Variables can store data of different types, and different data types
can do different things.
• String
• Integer
• Float (floating point numbers - also called double)
• Boolean
• Array
• Object
• NULL
• Resource
PHP String
String Functions
• The PHP strlen() function returns the length of a string.
• The PHP str_word_count() function counts the number of words in a
string
• The PHP strrev() function reverses a string
• The PHP strpos() function searches for a specific text within a string.
• The PHP str_replace() function replaces some characters with some
other characters in a string.
Example
PHP Integer
• An integer data type is a non-decimal number between -
2,147,483,648 and 2,147,483,647.
• Rules for integers:
• 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 three formats: decimal (10-based), hexadecimal
(16 based - prefixed with 0x) or octal (8-based - prefixed with 0)
• In the following example $x is an integer. The PHP var_dump() function
returns the data type and value:
Example
PHP Float
• A float (floating point number) is
a number with a decimal point
or a number in exponential
form.
• In the following example $x is a
float. The PHP var_dump()
function returns the data type
and value:
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:
Variables
• Variables are "containers" for storing
information.
• Creating (Declaring) PHP Variables
• In PHP, a variable starts with the $ sign, followed
by the name of the variable:
Rules for 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_x0002_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 Variables Scope
• PHP has three different variable scopes:
• local
• global
• static
•Global and Local Scope
• A variable declared outside a function has a GLOBAL SCOPE and can only be accessed outside
a function:
• A variable declared within a function has a LOCAL SCOPE and can only be accessed within that
function:
Example
PHP Operators
• Arithmetic operators
• Assignment operators
• Comparison operators
• Increment/Decrement operators
• Logical operators
• String operators
• Array operators
Example
<?php
$x=100;
$y=60;
echo "The sum of x and y is : ". ($x+$y) ."<br />";
echo "The difference between x and y is : ". ($x-$y) ."<br />";
echo "Multiplication of x and y : ". ($x*$y) ."<br />";
echo "Division of x and y : ". ($x/$y) ."<br />";
echo "Modulus of x and y : " . ($x%$y) ."<br />";
?>
PHP Conditional Statements
if Syntax
if (condition) {
code to be executed if condition is true;
}
The if...else Statement
Syntax:
if (condition) {
code to be executed if
condition is true;
} else {
code to be executed if
condition is false;
}
The if...elseif ....else Statement
PHP Switch Statement
Example
PHP Loops
The PHP while Loop
The PHP for Loop
The PHP foreach Loop

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy