You are on page 1of 11

PHP Language Basics

Building blocks of PHP


• PHP building blocks includes
1. Variables , which store and manipulate data in our scripts.
2. Data types , including which types are available in PHP, and how to test for
and change type.
3. Operators , which we can use to manipulate information.
4. Constants , which are useful for storing data that doesn’t change in our
script.

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

• Creating / Declaring Variables


- Creating a variable in PHP is known as declaring it.
- Declaring a variable is as simple as using its name in script:
- Syntax
$variable_name;

- Example

$first_variable;

- When PHP first sees a variable’s name in a script, it automatically


creates the variable at that point.
- Many programming languages prevent you from using a variable
without first explicitly declaring (creating) it. But PHP lets you use
variables at any point just by naming them. it is a helpful feature.

• 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;

- This creates the variable called $first_variable, and uses the =


operator to assign it a value of 3.
- We can perform declaration and initialization of a variable at a time.
Example
$first_variable=”BCS”;
$roll_no=112;
$percentage = 88.60;
- The following script creates two variables, initializes them with the
values 5 and 6 , then outputs their sum ( 11 ):

<?php
$x = 5;
$y = 6;
echo $x + $y;
?>

- You can see the result like following figure.


2. Data types
- All data stored in PHP variables fall into one of eight basic categories,
known as data types .
- A variable’s data type determines what operations can be carried out
on the variable ’s data, as well as the amount of memory needed to
hold the data.
- PHP provides eight types of values, or data types.
i) Four are scalar (single-value) types: integers, floating-point
numbers, strings, and booleans.
ii) Two are compound (collection) types: arrays and objects.
iii) The remaining two are special types: resource and NULL.

- 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);
?>

- You can see the result like following figure.

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;
?>

- You can see the result like following figure.

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:

$x = 5; // $x has a true value


$x = true; // clearer way to write it
$y = ""; // $y has a false value
$y = false; // clearer way to write it

- Compound types. Compound data is data that can contain more


than one value. The following table describes PHP ’ s compound
types:

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;
?>

▪ You can see the result like following figure.


- Special data types
▪ Special data types don’t contain scalar or
compound data as such, but have a specific
meaning:

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

// Open a file for reading

$handle = fopen("note.txt", "r");

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);
?>

▪ the result like following figure


• PHP as Loosely Typed Language
- PHP is known as a loosely - typed language. This means that it’s not
particularly fussy about the type of data stored in a variable.
- It converts a variable’s data type automatically, depending on the
context in which the variable is used.
- For example, we can initialize a variable with an integer value; add a
float value to it, thereby turning it into a float; then join it onto a
string value to produce a longer string.
- In contrast, many other languages, such as Java, are strongly – typed
; once we set the type of a variable in Java, it must always contain
data of that type.
- PHP’s loose typing is both good and bad. On the positive side, it
makes variables very flexible; the same variable can easily be used in
different situations. It also means that you don’t need to worry about
specifying the type of a variable when you declare it.
- However On the negative side, PHP won’t tell you if you accidentally
pass around data of the wrong type. For example, PHP will happily let
we pass a floating - point value to a piece of code that expects to be
working on an integer value. We probably won’t see an error
message, but you may discover that the output of our script isn’t
quite what we expected! These types of errors can be hard to track
down.

You might also like