PHP Data Types And Operators

Data Types:-

Related image


-Variables can store data of diferent types, and diferent data types can do diferent things.
PHP supports the following data types:
– String
– Integer
– Float (foating point numbers - also called double)
– Boolean
– Array
– Object
– NULL


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


PHP 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 specifed in three formats: decimal (10-based), hexadecimal (16-based - prefxed with 0x) or octal (8-based - prefxed with 0)

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

PHP Float:-
-A foat (foating point number) is a number with a decimal point or a number in exponential form.
In the following example $x is a foat. The PHP var_dump() function returns the data type and value:

<?php
$x = 10.365;
var_dump($x);
?>





PHP Boolean:-
A Boolean represents states: TRUE or FALSE.

$x = true;
$y = false;

Booleans are often used in conditional
testing.


PHP Null Value:-
- 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 = "Hello world!";
$x = null;
var_dump($x);
?>

PHP Constants:-

- Constants are like variables except that once they are defned they cannot be changed or undefned.
-A constant is an identifer (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).
-Unlike variables, constants are automatically global across the entire script. 


PHP Constant
- To create a constant, use the defne() function.
- Syntax
define(name, value, case-insensitive)
- constant name should be case-insensitive. Default is false

<?php
defne("GREET", "Welcome TY!");
echo GREET;
?>


PHP Operators:-

Image result for PHP Operators

-Operators are used to perform operations on variables and values.
-PHP divides the operators in the following groups:
- Arithmetic operators
- Assignment operators
- Comparison operators
- Increment/Decrement operators
- Logical operators
- String operators
- Array operators




String Operators:-

-PHP has two operators that are specially designed for strings.
.
- Used for concatenation
- $txt1 . $txt2
- Concatenation of $txt1 and $txt2
.=
- Used for concatenation and assignment
- $txt1 .= $txt2
- Appends $txt2 to $txt1

PHP – Include Files:-
You can include the content of a PHP fle into another PHP file before the server executes it.

There are two PHP functions which can be used to included one PHP fle into another PHP fle.
– The include() Function
– The require() Function
- The include (or require) statement takes all the text/code/ markup that exists in the specifed fle and copies it into the file that uses the include statement.
- Including files is very useful when you want to include the same PHP, HTML, or text on multiple pages of a website.


Mathematical Functions in PHP:-
- Abs()
- Ceil()
- Floor()
- Max()
- Min()
- Pi()
- Pow()
- round()
- Sqrt()
- rand()


abs() Function:-
Return the numbers: absolute value of diferent
<?php
echo(abs(6.7) . ".<br>".); //6.7
echo(abs(-6.7) . ".<br>".); //6.7
echo(abs(-3) . ".<br>".); // 3
echo(abs(3)); //3
?>


Ceil() function:-

Round numbers up to the nearest integer
<?php
echo(ceil(0.60) . ".<br>".); //1
echo(ceil(0.40) . ".<br>".); //1
echo(ceil(5) . ".<br>".); //5
echo(ceil(5.1) . ".<br>".); //6
echo(ceil(-5.1) . ".<br>".); //-5
echo(ceil(-5.9)); //-5
?>


Floor() function:-

Round numbers down to the nearest integer
<?php
echo(foor(0.60) . ".<br>".); //0
echo(foor(0.40) . ".<br>".); //0
echo(foor(5) . ".<br>".); //5
echo(foor(5.1) . ".<br>".); //5
echo(foor(-5.1) . ".<br>".); -6
echo(foor(-5.9)); //-6
?>

Max() function:-

Find highest value with the max() function
<?php
echo(max(2,4,6,8,10) . ".<br>".); //10
echo(max(22,14,68,18,15) . ".<br>".); //68
echo(max(array(4,6,8,10)) . ".<br>".); //10
echo(max(array(44,16,81,12))); //81
?>

Min() Function:-

Find lowest value with the min() function
<?php
echo(min(2,4,6,8,10) . ".<br>".); //2
echo(min(22,14,68,18,15) . ".<br>".); //14
echo(min(array(4,6,8,10)) . ".<br>".); //4
echo(min(array(44,16,81,12))); //12
?>

Pi() function:-

Return the value of PI:
<?php
echo(pi()); //3.1415926535898
?>

Pow() function:-

The pow() function returns x raised to the
power of y.
<?php
echo(pow(2,4) . ".<br>".); //16
echo(pow(-2,4) . ".<br>".); //16
echo(pow(-2,-4) . ".<br>".); //0.0625
echo(pow(-2,-3.2)); //NAN
?>

Round() function:-
- The round() function rounds a foating-point number.
- To round a number UP to the nearest integer, look at the ceil() function.
- To round a number DOWN to the nearest integer, look at the foor()
function.
<?php
echo(round(0.60) . ".<br>".); //1
echo(round(0.50) . ".<br>".); //1
echo(round(0.49) . ".<br>".); //0
echo(round(-4.40) . ".<br>".); //-4
echo(round(-4.60)); //-5
?>

Rand() function:-

Generate random numbers
<?php
echo(rand() . ".<br>".); // 10086
echo(rand() . ".<br>".); //19034
echo(rand(10,100)); //74
?>
-If you want a random integer between 10 and 100 (inclusive), use rand (10,100).

Sqrt() function:-

Return the square root of diferent numbers
<?php
echo(sqrt(0) . ".<br>".); //0
echo(sqrt(1) . ".<br>".); //1
echo(sqrt(9) . ".<br>".); //3
echo(sqrt(0.64) . ".<br>".); //0.8
echo(sqrt(-9)); //NAN
?>

Post a Comment

0 Comments