unit 2 text and number
unit 2 text and number
Variables in PHP
Variables are used for storing value, like strings, numbers or arrays. When a variable is declared,
it can be used over and over again in your script. All variable in PHP start with a $ sign symbol.
Syntax:
$var_name=value;
Let’s try creating a variable containing string and variable containing a number
<?php
$txt=”hello world”;
$x=32;
?>
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)
In the example above, notice that we did not have to tell PHP which data type the variable is.
PHP automatically associates a data type to the variable, depending on its value. Since the
data types are not set in a strict sense, you can do things like adding a string to an integer
without causing an error.
PHP 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).
1
To create a constant, use the define() function.
Syntax
define(name, value, case-insensitive)
Parameters:
Example
<?php
define("GREETING", "Welcome to W3Schools.com!");
echo GREETING;
?>
Example
<?php
define("GREETING", "Welcome to W3Schools.com!", true);
echo greeting;
?>
String
Integer
Float (floating point numbers - also called double)
Boolean
Array
Object
NULL
Resource
2
PHP String
A string can be any text inside quotes. You can use single or double quotes:
Example
<?php
$x = "Hello world!";
$y = 'Hello world!';
echo $x;
echo "<br>";
echo $y;
?>
PHP Integer
In the following example $x is an integer. The PHP var_dump() function returns the data type
and value:
Example
<?php
$x = 5985;
var_dump($x);
?>
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:
3
Example
<?php
$x = 10.365;
var_dump($x);
?>
PHP Boolean
<?php $x = true;
$y = false;
?>
PHP Array
In the following example $cars is an array. The PHP var_dump() function returns the data type
and value:
Example
<?php
$cars = array("Volvo","BMW","Toyota");
var_dump($cars);
?>
PHP Object
An object is a data type which stores data and information on how to process that data.
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:
Example
<?php
class Car {
function Car() {
$this->model = "VW";
}
}
4
// create an object
$herbie = new Car();
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.
Example
<?php
$x = "Hello world!";
$x = null;
var_dump($x);
?>
Strings
Strings can be seen as stream of characters. For example , ‘G’ is a character and ‘BIM NOTES’
is string. A string is a sequence of characters, like "Hello world!".
Built-in function in PHP are some existing library function which can be used directly in our
programs making an appropriate call to them. Below are some important built-in string function
that we use in our daily and regular program.
Example
5
<?php
echo strlen("Hello world!"); // outputs 12
?>
Example
<?php
echo str_word_count("Hello world!"); // outputs 2
?>
Example
<?php
echo strrev("Hello world!"); // outputs !dlrow olleH
?>
The PHP strpos() function searches for a specific text within a string. If a match is found, the
function returns the character position of the first match. If no match is found, it will return
FALSE.
Example
<?php
echo strpos("Hello world!", "world"); // outputs 6
?>
The PHP str_replace() function replaces some characters with some other characters in a string.
6
Example
<?php
echo str_replace("world", "Dolly", "Hello world!"); // outputs Hello Dolly!
?>
Related functions:
Example
<?php
echo strtolower("Hello WORLD.");
?>
Example
<?php
echo strtoupper("Hello WORLD!");
?>
Syntax
trim(string,charlist)
7
Example
Remove characters from both sides of a string ("He" in "Hello" and "d!" in "World"):
<?php
$str = "Hello World!";
echo $str . "<br>";
echo trim($str,"Hed!");
?>
PHP Numbers
One thing to notice about PHP is that it provides automatic data type conversion.
So, if you assign an integer value to a variable, the type of that variable will automatically be an
integer. Then, if you assign a string to the same variable, the type will change to a string.
PHP Integers
2, 256, -256, 10358, -179567 are all integers. While 7.56, 10.0, 150.67 are floats.
So, an integer data type is a non-decimal number between -2147483648 and 2147483647. A
value greater (or lower) than this, will be stored as float, because it exceeds the limit of an
integer.
PHP has the following functions to check if the type of a variable is integer:
is_int()
is_integer() - alias of is_int()
is_long() - alias of is_int()
Example
<?php
$x = 5985;
var_dump(is_int($x));
$x = 59.85;
var_dump(is_int($x));
?>
8
PHP Floats
The float data type can commonly store a value up to 1.7976931348623E+308 (platform
dependent), and have a maximum precision of 14 digits.
PHP has the following functions to check if the type of a variable is float:
is_float()
is_double() - alias of is_float()
Example
<?php
$x = 10.365;
var_dump(is_float($x));
?>
PHP Infinity
PHP has the following functions to check if a numeric value is finite or infinite:
is_finite()
is_infinite()
However, the PHP var_dump() function returns the data type and value:
Example
<?php
$x = 1.9e411;
var_dump(is_infinite($x));
?>
9
PHP NaN
is_nan()
However, the PHP var_dump() function returns the data type and value:
Example
<?php
$x = acos(8);
var_dump(is_nan($x));
?>
The PHP is_numeric() function can be used to find whether a variable is numeric. The function
returns true if the variable is a number or a numeric string, false otherwise.
Example
<?php
$x = 5985;
var_dump(is_numeric($x));
$x = "5985";
var_dump(is_numeric($x));
$x = "59.85" + 100;
var_dump(is_numeric($x));
$x = "Hello";
var_dump(is_numeric($x));
?>
10
PHP Casting Strings and Floats to Integers
Sometimes you need to cast a numerical value into another data type.
The (int), (integer), or intval() function are often used to convert a value to an integer.
Example
<?php
// Cast float to int
$x = 23465.768;
$int_cast = (int)$x;
echo $int_cast;
echo "<br>";
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
The PHP arithmetic operators are used with numeric values to perform common arithmetical
operations, such as addition, subtraction, multiplication etc.
11
Operator Name Example Result
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
12
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):
=== Identical $x === Returns true if $x is equal to $y, and they are of the
$y same type
!== Not identical $x !== $y Returns true if $x is not equal to $y, or they are not of
the same type
13
or equal to
<=> Spaceship $x <=> Returns an integer less than, equal to, or greater than
$y zero, depending on if $x is less than, equal to, or greater
than $y. Introduced in PHP 7.
14
and And $x and $y True if both $x and $y are true
PHP has two operators that are specially designed for strings.
15
Operator Name Example Result
=== Identity $x === $y Returns true if $x and $y have the same key/value pairs
in the same order and of the same types
The PHP conditional assignment operators are used to set a value depending on conditions:
16
value of $x is expr2.
Introduced in PHP 7
17