Unit 1 - VP
Unit 1 - VP
PROGRAMMING
USING PHP
2 1 B C A 2 T 4 6 1 - C R ED I T S : 4
TO TA L H O U R S : 6 0 C O U R S E T Y P E: D S C
PHP script is executed much faster than PHP has easily understandable syntax.
those scripts which are written in other Programmers are comfortable coding
languages such as JSP and ASP. with it.
Open Source:
PHP is available for WINDOWS, MAC, PHP has predefined error reporting constants
LINUX & UNIX operating system. A PHP to generate an error notice or warning at
application developed in one OS can be runtime. E.g., E_ERROR, E_WARNING,
E_STRICT, E_PARSE.
easily executed in other OS also.
9
Features of PHP
PHP is compatible with almost all local Different programming languages require
servers used today like Apache, long script or code, whereas PHP can do the
same work in a few lines of code. It has
Netscape, Microsoft IIS, etc.
maximum control over the websites like you
can make changes easily whenever you want.
o Artisan Console
o Middleware
o Routing
o Laravel Mix
Laravel Mix: Laravel Mix is a tool provided by Laravel for simplifying the process of
managing and compiling assets, such as JavaScript and CSS files, in web applications.
It's built on top of Webpack, which is a popular module bundler for modern JavaScript
applications.
Task Scheduling and Queues: Laravel provides a convenient way to schedule tasks
using the task scheduler. Queues allow you to defer the processing of time-consuming
tasks, improving the responsiveness of your application.
VISHNU PRIYA P M | WEB PROGRAMMING USING PHP 12/23/2024 14
Installation of php
To install PHP on your system, you can follow different steps depending
on your operating system. Here are instructions for the most common
operating systems:
For Windows:
Using XAMPP:
Download and install XAMPP, which includes PHP, Apache, MySQL, and other
components.
Follow the installation wizard to set up XAMPP.
Start the Apache server.
Using WampServer:
To run a PHP script, create a file with a .php extension, for example, hello.php, and add
the following content:
<?php
echo "Hello, World!";
?>
Save the file and run it using the following command in the terminal or command
prompt:
php hello.php
22
Executing PHP and viewing in browser
● PHP File
● Save in htdocs
● Open browser
● Local Host
● Open the file path
23
Embedding PHP and HTML
<html>
<title>HTML with PHP</title>
<body>
<h1>My Example</h1>
<?php
//your PHP code goes here
?>
<b>Here is some more HTML</b>
<?php
//more PHP code
?>
</body>
</html>
24
PHP Variables
In PHP, a variable starts with the $ sign, followed by the name of the variable:
<?php
$x = 5;
$y = 10.5;
?>
25
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 _ )
● Variable names are case-sensitive ($age and $AGE are two different
variables)
<?php
$txt = "Kristu Jayanti College";
echo "I love $txt!";
?>
27
Output Variables
<?php
$txt = "Kristu Jayanti College";
echo "I love " . $txt . "!";
?>
28
Output Variables
<?php
$x = 5;
$y = 4;
echo $x + $y;
?>
29
PHP Data Types
● String
● Integer
● Float (floating point numbers - also called double)
● Boolean
● Array
● Object
● NULL
30
PHP Data Types
PHP String
● A string is a sequence of characters, like "Hello world!".
● A string can be any text inside quotes. You can use single or double quotes:
<?php
$x = "Hello world!";
$y = 'Hello world!';
echo $x;
echo "<br>";
echo $y;
31
?>
PHP Data Types
PHP Integer
An integer data type is a non-decimal number between -2,147,483,648 and 2,147,483,647.
32
PHP Data Types
● In the following example $x is an integer. The PHP var_dump() function returns the data type and value:
<?php
$x = 5985;
var_dump($x);
?>
var_dump() provides detailed information about each variable, including its data
type and value.
33
PHP Data Types
PHP Float
A float (floating point number) is a number with a decimal point or a number in exponential form.
<?php
$x = 10.365;
var_dump($x);
?>
34
PHP Data Types
PHP Boolean
A Boolean represents two possible states: TRUE or FALSE.
$x = true;
$y = false;
35
PHP Data Types
PHP Array
An array stores multiple values in one single variable.
<?php
$cars = array("Volvo","BMW","Toyota");
var_dump($cars);
?>
36
PHP Data Types
PHP Object
Classes and objects are the two main aspects of object-oriented programming.
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.
Let's assume we have a class named Car. A Car 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.
37
PHP Data Types
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);
38
?>
PHP Comments
<?php
// This is a single-line comment
# This is also a single-line comment
?>
39
PHP Comments
40
Output?
<?php
// You can also use comments to leave out parts of a code line
$x = 5 /* + 15 */ + 5;
echo $x;
?>
41
PHP Operators
PHP Operators
Operators are used to perform operations on variables and values.
● Arithmetic operators
● Assignment operators
● Comparison operators
● Increment/Decrement operators
● Logical operators
● String operators
● Array operators
● Conditional assignment operators
42
PHP Arithmetic Operators
The PHP arithmetic operators are used with numeric values to perform common arithmetical operations, such as
addition, subtraction, multiplication etc.
43
PHP Assignment Operators
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.
44
PHP Assignment Operators
<?php
$x = 15;
$x %= 4;
echo $x;
?>
45
PHP Comparison Operators
spaceship operator
46
PHP Comparison Operators
spaceship operator
The spaceship operator or combined comparison operator is denoted by “<=>“. This is a three-way
comparison operator and it can perform greater than, less than and equal comparison between two
operands.
This operator has similar behavior like strcmp() . This operator can be used with integers, floats,
strings, arrays, objects, etc.
This <=> operator offers combined comparison :
47
PHP Comparison Operators
The PHP comparison operators are used to compare two values (number or string):
● Example:
● // Comparing Integers
● // String Comparison
48
PHP Comparison Operators
The PHP comparison operators are used to compare two values (number or string):
<?php <?php
$x = 100; $x = 100;
$y = "100"; $y = "100";
50
<?php <?php
$x = 10; $x = 10;
echo ++$x; echo $x++;
?> ?>
51
PHP Logical Operators
The PHP logical operators are used to combine conditional statements.
52
PHP Logical Operators
The PHP logical operators are used to combine conditional statements.
<?php
$x = 100;
$y = 50;
?>
53
PHP Logical Operators
The PHP logical operators are used to combine conditional statements.
<?php
$x = 100;
$y = 50;
?>
54
PHP Logical Operators
The PHP logical operators are used to combine conditional statements.
<?php
$x = 100;
$y = 50;
?>
55
PHP String Operators
PHP has two operators that are specially designed for strings.
<?php <?php
$txt1 = "Hello"; $txt1 = "Hello";
$txt2 = " world!"; $txt2 = " world!";
echo $txt1 . $txt2; $txt1 .= $txt2;
?> echo $txt1;
?>
56
Access Specifiers in PHP
There are 3 types of Access Specifiers available in PHP, Public, Private and
Protected.
• Private - class members with this keyword will be accessed within the
class itself. It protects members from outside class access with the
reference of the class instance.
}
$obj= new child;
$obj->add();
$obj->sub();