UNIT 1-WT-PHP-IIB.COM(CA)
UNIT 1-WT-PHP-IIB.COM(CA)
WEB TECHNOLOGY(PHP)
UNIT- I
Introduction to PHP – Basic Development concepts-Creating First PHP scripts – Using
Variables and Operators- Storing data in Variable –Understanding data types- Setting and
Checking Variables Data types-Using Constants-Manipulating Variables with operators
Introduction to PHP
PHP was created by Rasmus Lerdorf in 1994 but appeared in the market in 1995. PHP
7.4.0 is the latest version of PHP, which was released on 28 November. Some important
points need to be noticed about PHP are as followed:
use PHP
PHP is a server-side scripting language, which is used to design the dynamic web
applications with MySQL database.
o It handles dynamic content, database as well as session tracking for the website.
o You can create sessions in PHP.
o It can access cookies variable and also set cookies.
o It helps to encrypt the data and apply validation.
PHP Features
performance:
PHP script is executed much faster than those scripts which are written in other languages
such as JSP and ASP. PHP uses its own memory, so the server workload and loading time is
automatically reduced, which results in faster processing speed and better performance.
Open Source:
PHP source code and software are freely available on the web. You can develop all the
versions of PHP according to your requirement without paying any cost. All its components
are free to download and use.
Web Development
PHP is widely used in web development nowadays. PHP can develop dynamic websites
easily. But you must have the basic the knowledge of following technologies for web
development as well.
o HTML
o CSS
o JavaScript
o Ajax
o XML and JSON
o jQuery
Prerequisite
Before learning PHP, you must have the basic knowledge of HTML, CSS, and JavaScript.
So, learn these technologies for better implementation of PHP.
A PHP file contains HTML tags and some PHP scripting code. It is very easy to create a
simple PHP example.
Create a file and write HTML tags + PHP code
1. <?php
2. //your code here
3. ?>
1. <!DOCTYPE>
2. <html>
3. <body>
4. <?php
5. echo "<h2>Hello First PHP</h2>";
6. ?>
7. </body>
8. </html>
Output:
Example:2
1. <!DOCTYPE>
2. <html>
3. <body>
4. <?php
5. echo "Hello world using echo </br>";
6. ECHO "Hello world using ECHO </br>";
7. EcHo "Hello world using EcHo </br>";
8. ?>
9. </body>
PHP echo is a language construct, not a function. Therefore, you don't need to use parenthesis
with it. But if you want to use more than one parameter, it is required to use parenthesis.
Example:1
1. <?php
2. echo "Hello by PHP echo";
3. ?>
Output:
Hello by PHP echo
Example:2
1. <?php
2. $msg="Hello JavaTpoint PHP";
3. echo "Message is: $msg";
4. ?>
Output:
Message is: Hello JavaTpoint PHP
When such an HTML documents is requested by user, a PHP aware Web server can
recognize and execute the PHP code blocks and interpolate the resulting output into the
HTML documents before returning it to the requesting user. The result: a Web page or
application that almost seems alive, responding intelligently to user actions by virtue of PHP
program logic embedded within it. Fig 1-1 illustrates the process, showing the four elements
of the LAMP framework, described later in this section Here’s what’s happening in fig1-1:
1. Joe pops open his Web browser at home and types in the URL to a Web site. After
looking up domain, Joe’s browser(the client) sends an HTTP request to the
corresponding server IP address.
2. The Web server handling HTTP request for the domain receives the request and notes
that URL end with a .php suffix. Because the server is programmed to automatically
redirect all such requests to the PHP layer, it simply invokes the PHP interpreter and
passes it the contents of the named file.
3. The PHP interpreter parses the file, executing the code in the special PHP tags. Within
these tags, you can perform calculations, process user input, interact with a database,
read and writes files… the list goes on! Once the script interpreter has completed
executing the PHP instructions, it returns the result to the browser, clean up after itself,
and goes back into hibernation.
4. The results retuned by the interpreter are transmitted to Joe’s browser by the Web server.
From the preceding explanation, it should be clear that to get started building PHP
applications, your development environment must contains at least three components:
A base operating system(OS) and server environment(usually Linux).
A Web server(usually Apache on Linux or IIS on Windows) to intercept HTTP requests
and either serve them directly or pass them on to the PHP interpreter for execution.
To get started, you'll need a text editor to write your PHP code. You can use any text editor
you prefer, such as Notepad++, Sublime Text, or Visual Studio Code. Once you have your
text editor set up, follow the steps below to create your first PHP script:
Start by creating a new file with a .php extension, such as "first_script.php". This
extension is necessary to indicate that the file contains PHP code.
In PHP, you need to enclose your code within opening and closing PHP tags. These
tags allow the PHP interpreter to identify and execute the PHP code within them.
Open your PHP file and add the following tags:
<?php
//your code here
?>
Now you can start writing your PHP script within the PHP tags. Here's an example of
a simple PHP script that prints "Hello, World!" to the browser:
<?php
echo "Hello by PHP echo";
In this script, the echo statement is used to output the string "Hello, World!" to the
browser. The semicolon (;) at the end of the line indicates the end of the statement.
To run your PHP script, you'll need a web server with PHP installed. You can either install a
local server like XAMPP or WAMP on your computer or use an online development
environment like Replit or PHPFiddle.
If you're using a local server, make sure to place your PHP file in the appropriate directory,
typically the "htdocs" folder for XAMPP or the "www" folder for WAMP. Then, access your
script through the browser by entering the server's URL followed by the name of your PHP
file (e.g., http://localhost/first_script.php).
Comments in PHP
A comment in PHP code is a line that is not executed as a part of the program. Its only
purpose is to be read by someone who is looking at the code.
/* This is a
multi-line comment */
Any text between // and the end of the line will be ignored (will not be executed).
Example:
Output:
Welcome Home
<!DOCTYPE html>
<html>
<body>
<?php
/*
The next statement will
print a welcome message
*/
echo "Welcome Home!";
?>
</body>
</html>
Output:
Welcome Home!
PHP Variables
A variable can have a short name (like $x and $y) or a more descriptive name
($age, $carname, $total_volume).
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 _ )
Example:
<!DOCTYPE html>
<html>
<body>
<?php
$txt = "how are you";
echo "Hai $txt!";
?>
</body>
</html>
Output:
Example:
<!DOCTYPE html>
<html>
<body>
<?php
$x = 5;
$y = 4;
echo $x + $y;
?>
</body>
</html>
Output:
PHP automatically associates a data type to the variable, depending on its value
Variable Types
PHP has no command for declaring a variable, and the data type depends on the value of the
variable
!DOCTYPE html>
<html>
<body>
<?php
$x = 5; // $x is an integer
$y = "John"; // $y is a string
Output:
5john
Example
The var_dump() function returns the data type and the value:
!DOCTYPE html>
<html>
<body>
<?php
$x = 5;
var_dump($x);
?>
</body>
</html>
Output:
int(5)
Assigning a string to a variable is done with the variable name followed by an equal sign and
the string:
<!DOCTYPE html>
<html>
<body>
<?php
$x = "John";
echo $x;
?>
</body>
</html>
Output:
John
You can assign the same value to multiple variables in one line:
Arithmetic operators
Assignment operators
Comparison operators
Increment/Decrement operators
Logical operators
String operators
Array operators
Conditional assignment operators
The PHP arithmetic operators are used with numeric values to perform common arithmetical
operations, such as addition, subtraction, multiplication etc.
The PHP assignment operators are used with numeric values to write a value to a variable.
The basic assignment operator in PHP is "=". It means that the left operand gets set to the
value of the assignment expression on the right.
x=y x=y The left operand gets set to the value of the
expression on the right
x += y x=x+y Addition
x -= y x=x-y Subtraction
x *= y x=x*y Multiplication
x /= y x=x/y Division
x %= y x=x%y Modulus
The PHP comparison operators are used to compare two values (number or string):
!== Not identical $x !== $y Returns true if $x is not equal to $y, or they
are not of the same type
PHP has two operators that are specially designed for strings.
The PHP conditional assignment operators are used to set a value depending on conditions:
Example sets the value of the variable alpha . The value generated on the right side of the
assignment statement is stored in the variable.
The value generated on the right side of the assignment statement is stored in the
variable.
Assigning a string to a variable is done with the variable name followed by an equal sign and
the string:
<!DOCTYPE html>
<html>
<body>
<?php
$x = "John";
echo $x;
?>
</body>
</html>
OUTPUT
You can assign the same value to multiple variables in one line:
<!DOCTYPE html>
<html>
<body>
<?php
$x = $y = $z = "Fruit";
echo $x;
echo $y;
echo $z;
?>
</body>
</html>
OUTPUT
FruitFruitFruit
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
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!';
var_dump($x);
echo "<br>";
Output:
PHP Integer
In the following example $x is an integer. The PHP var_dump() function returns the data type
and value:
<!DOCTYPE html>
<html>
<body>
<?php
$x = 5985;
var_dump($x);
?>
</body>
</html>
Output:
int(5985)
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:
<!DOCTYPE html>
<html>
<body>
<?php
$x = 10.365;
var_dump($x);
PHP Boolean
<!DOCTYPE html>
<html>
<body>
<?php
$x = true;
var_dump($x);
?>
</body>
</html>
Output:
bool(true)
PHP Array
In the following example $cars is an array. The PHP var_dump() function returns the data
type and value:
<!DOCTYPE html>
<html>
<body>
<?php
$cars = array("Volvo","BMW","Toyota");
var_dump($cars);
?>
</body>
</html>
Output:
PHP Object
Classes and objects are the two main aspects of object-oriented programming.
Let's assume we have a class named Car that can have properties like model, color, etc. We
can define variables like $model, $color, and so on, to hold the values of these properties.
When the individual objects (Volvo, BMW, Toyota, etc.) are created, they inherit all the
properties and behaviors from the class, but each object will have different values for the
properties.
If you create a __construct() function, PHP will automatically call this function when you
create an object from a class.
<!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 . "!";
}
}
</body>
</html>
Output:
object(Car)#1 (2) { ["color"]=> string(3) "red" ["model"]=> string(5) "Volvo" }
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.
<?php
$x = "Hello world!";
$x = null;
var_dump($x);
?>
</body>
</html>
Output:
NULL
If you assign an integer value to a variable, the type will automatically be an integer.
If you assign a string to the same variable, the type will change to a string:
<!DOCTYPE html>
<html>
<body>
<?php
$x = 5;
var_dump($x);
echo "<br>";
$x = "Hello";
var_dump($x);
?>
</body>
</html>
Output:
int(5)
string(5) "Hello"
PHP Resource
The special resource type is not an actual data type. It is the storing of a reference to
functions and resources external to PHP.
1. Declare: Use the declare statement to set the data type of a variable.
$x = 5; // Integer
$y = 'hello'; // String
$x = 5;
echo gettype($x); // Outputs: "integer"
$x = 5;
if (is_int($x)) { echo "x is an integer"; }
$x = 5;
var_dump($x); // Outputs: "int(5)"
- is_int()
- is_float()
- is_string()
- is_bool()
- is_array()
- is_object()
- is_null()
By setting and checking variable data types, you can ensure that your code is working with
the correct data types, which can help prevent errors and improve code reliability.
These functions are useful for validating user input, checking the type of a variable, and
ensuring that your code is working with the correct data types.
Using Constants
A constant is an identifier (name) for a simple value. The value cannot be changed during the
script.
A valid constant name starts with a letter or underscore (no $ sign before the constant name).
Note: Unlike variables, constants are automatically global across the entire script.
Syntax
define(name, value, case-insensitive);
Parameters:+
Output:
Welcome to W3Schools.com!
HP const Keyword
Example
<!DOCTYPE html>
<html>
<body>
<?php
const MYCAR = "Volvo";
echo MYCAR;
?>
</body>
</html>
Output:
Volvo
Example
<?php
define("cars", [
"Alfa Romeo",
"BMW",
"Toyota"
]);
echo cars[0];
?>
</body>
</html>
Output:
Alfa Romeo
Constants are automatically global and can be used across the entire script.
Example
This example uses a constant inside a function, even if it is defined outside the function:
<!DOCTYPE html>
<html>
<body>
<?php
define("GREETING", "Welcome to W3Schools.com!");
function myTest() {
echo GREETING;
}
myTest();
?>
</body>
</html>
Ouput:
Welcome to W3Schools.com!
- $x = 5 + 3; (Addition) - $x is now 8
Assignment Operators
- $x = 5; (Assignment) - $x is now 5
- $x += 3; (Addition and Assignment) - $x is now 8
- $x -= 3; (Subtraction and Assignment) - $x is now 2
- $x *= 3; (Multiplication and Assignment) - $x is now 15
- $x /= 3; (Division and Assignment) - $x is now 1.67
- $x %= 3; (Modulus and Assignment) - $x is now 2
- $x **= 3; (Exponentiation and Assignment) - $x is now 125
Comparison Operators
Logical Operators
- if ($x > 5 && $y > 3) { echo "x is greater than 5 and y is greater than 3"; } (And) - No
output
- if ($x > 5 || $y > 3) { echo "x is greater than 5 or y is greater than 3"; } (Or) - Outputs: "x is
greater than 5 or y is greater than 3"
- if (!$x) { echo "x is false"; } (Not) - Outputs: "x is false"
String Operators
Increment/Decrement Operators
Addition
<!DOCTYPE html>
Output:
16
Subtraction
<!DOCTYPE html>
<html>
<body>
<?php
$x = 10;
$y = 6;
echo $x - $y;
?>
</body>
</html>
Output:
4
Multiplication
<!DOCTYPE html>
<html>
<body>
<?php
$x = 10;
$y = 6;
echo $x * $y;
?>
</body>
</html>
Output:
60
Division
<!DOCTYPE html>
<html>
<body>
<?php
$x = 10;
$y = 6;
echo $x / $y;
Output:
1.6666666666667
Modulus
<!DOCTYPE html>
<html>
<body>
<?php
$x = 10;
$y = 6;
echo $x % $y;
?>
</body>
</html>
Output:
4
Exponentiation
<!DOCTYPE html>
<html>
<body>
<?php
$x = 10;
$y = 3;
echo $x ** $y;
?>
</body>
</html>
Output:
1000