Web Development
Web Development
Introduction to PHP
SEM-4
What is PHP?
• PHP stands for Hypertext Preprocessor
• It’s a general purpose scripting language that is freely available
and widely used for web development.
• Server side scripting language.
• With PHP we create dynamic websites.
• Latest vesion of PHP 8.1
• Extension .php
What is PHP?
• PHP -> HTML ------ CSS-------Java Script
• MySQL(Database)storageroom
PHP Syntax
• <? PHP
• ?>
XAMPP INSTALLATION
• Always save file with .php
It is a case sensitive
• PHP variables can store various types of data, such as strings,
numbers, arrays, objects, etc. The type of data a variable holds is
determined dynamically based on the value assigned to it.
• Here are a few examples with different data types:
In this example:
1.We declare and assign values to two variables, $greeting and $name.
2.We concatenate these variables to create a new variable called $message.
3.Finally, we use echo to output the value of $message.
• When you run this PHP code, it will output:
•Variable Types
• PHP has no command for declaring a variable, and the data type depends on the
value of the variable.
• PHP supports the following data types:
• Integer
• Float (floating point numbers also called double)
• Boolean
• Resource
• Array
• String
• Object
• Null
PHP Variables Scope(types)
• 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.
1. Local
2. Global
3. 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:
PHP Static
• 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:
Output
PHP String Output
PHP Float
PHP Boolean
PHP Array
• An array stores multiple value in one single variable.
• In the following example $cars is an array. The PHP var_dump()
function returns the data type and value:
Output
• 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.
• 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.
PHP Null
• Null is a special data type which can have only one value: NULL.
• We will not talk about the resource type here, since it is an advanced
topic.
PHP String
• Strings in PHP are surrounded by either double quotation marks, or single quotation marks.
• You can use double or single quotes, but you should be aware of the differences between
the two.
• Double quoted strings perform action on special characters. E.g. when there is a variable in
the string, it returns the value of the variable.
• Single quoted strings does not perform such actions, it returns the string like it was written,
with the variable name:
PHP Constants:- Constants are like variables, except that
once they are defined they cannot be changed or undefined.
• 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.