PHP Language Basics Building Blocks of PHP
PHP Language Basics Building Blocks of PHP
1. Variables in PHP
- A variable is simply a container that holds a certain value.
- Variables get their name because that certain value can change
throughout the execution of the script.
- It’s this ability to contain changing values that make variables so
useful.
- PHP variables are use to hold values or expressions.
- A variable can have a short name (like x and y) or a more
descriptive name (age, carname, total_volume).
• Naming Variables
- A variable consists of two parts:
i) the variable’s name and
ii) The variable’s value.
Because you’ll be using variables in your code frequently, it’s
best to give your variables names you can understand and
remember.
- PHP has certain rules for naming variables:
i) Variable names begin with a dollar sign ( $ ).
ii) The first character after the dollar sign must be a letter or an
underscore. The remaining characters in the name may be
letters(A-Z and a-z), numbers(0-9), or underscores (_)without a
fixed limit.
iii) A variable name cannot start with a number.
iv) Variable names are case - sensitive ( $Variable and $variable
are two distinct variables)
v) It’s also worth pointing out that variable names longer than 30
characters are somewhat impractical.
vi) Here are some examples of PHP variable names:
$first_variable
$anotherVariable
$x
$_123
- Example
$first_variable;
• Initializing Variables
- When you declare a variable in PHP, it’s good practice to assign a
value to it at the same time. This is known as initializing a variable.
- Syntax
$variable_name = Value;
- By doing this, anyone reading your code knows exactly what value
the variable holds at the time it’s created.
- Example of declaring and initializing a variable:
$first_variable = 3;
<?php
$x = 5;
$y = 6;
echo $x + $y;
?>
- Scalar data types. Scalar data means data that contains only a
single value. Here’s a list of them, including examples
i) 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:
<?php
$x = 5985;
echo $x.”<br>”;
var_dump($x);
?>
ii) Float :
- A float (floating point number) is a number with a
decimal point or a number in exponential form. Usually,
this allows numbers between 1.7E-308 and 1.7E+308
with 15 digits of accuracy.
- In the following example $x is a float. The PHP
var_dump() function returns the data type and value:
<?php
$x = 10.365;
echo $x.”<br>”;
var_dump($x);
?>
- You can see the result like following figure.
iii) 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.
- Example
<?php
$x = "Hello world!";
$y = 'Hello';
echo $x;
echo "<br>";
echo $y;
?>
iv) Boolean :
- A Boolean represents two possible states: TRUE or
FALSE.
- In PHP, the following values are false:
▪ The keyword false
▪ The integer 0
▪ The floating-point value 0.0
▪ The empty string ("") and the string "0"
▪ An array with zero elements
▪ An object with no values or functions
▪ The NULL value
- Any value that is not false is true.
- PHP provides true and false keywords for clarity:
i) Array
▪ An array stores multiple values in one single
variable.
▪ In the following example $cars is an array. The PHP
var_dump() function returns the data type and
value:
<?php
$cars= array("Volvo","BMW","Toyota");
var_dump($cars);
?>
▪ You can see the result like following figure.
ii) Object
▪ An object is a data type which stores data and
information on how to process that data.
▪ In PHP, an object must be explicitly declared.
▪ First we must declare a class of object.
▪ For this, we use the class keyword.
▪ A class is a structure that can contain
properties and methods:
<?php
class Car {
function Car() {
$this->model = "VW";
}
}
// create an object
$C = new Car();
// show object properties
echo $C->model;
?>
i) 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.
▪ A common example of using the resource data type
is a database call.
▪ Resource variables typically hold special handlers to
opened files and database connections.
▪ Example
<?php
var_dump($handle);
?>
▪ we can see the result like following figure
ii) Null
▪ 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.
▪ If a variable is created without a value, it is
automatically assigned a value of NULL.
▪ Variables can also be emptied by setting the value to
NULL.
<?php
$x = null;
var_dump($x);
?>