Thursday, 30 July 2015

PHP-DATA-TYPES AND OPEARTORS

02:35

What is PHP?
  • PHP is an acronym for "PHP: Hypertext Preprocessor"
  • PHP is a widely-used, open source scripting language
  • PHP scripts are executed on the server
  • PHP is free to download and use

What is a PHP File ?
  • PHP files can contain text, HTML, CSS, JavaScript, and PHP code
  • PHP code are executed on the server, and the result is returned to the browser as plain HTML
  • PHP files have extension ".php"

What Can PHP Do ?
  • PHP can generate dynamic page content
  • PHP can create, open, read, write, delete, and close files on the server
  • PHP can collect form data
  • PHP can send and receive cookies
  • PHP can add, delete, modify data in your database
  • PHP can be used to control user-access
  • PHP can encrypt data.

Why PHP?
  • PHP runs on various platforms (Windows, Linux, Unix, Mac OS X, etc.)
  • PHP is compatible with almost all servers used today (Apache, IIS, etc.)
  • PHP supports a wide range of databases
  • PHP is free. Download it from the official PHP resource: www.php.net
  • PHP is easy to learn and runs efficiently on the server side

Basic PHP Syntax:
A PHP script can be placed anywhere in the document.
A PHP script starts with <?php and ends with ?>:
<?php
// PHP code goes here
?>
Creating (Declaring) PHP Variables:
In PHP, a variable starts with the $ sign, followed by the name of the variable:
Example:
<?php

$txt = "Hello world!";
$x = 5;
$y = 10.5;
?>
Output:

(note if we will echo  $x and $y then we will get following output use of echo statement is given on next step..just now understand that it work similar to  printf fucntion in c , cout  in c++ ,System.out.println() in java )
Hello world!
5
10.5

PHP echo and print Statements

echo and print are more or less the same. They are both used to output data to the screen.
The differences are small: echo has no return value while print has a return value of 1 so it can be used in expressions. echo can take multiple parameters (although such usage is rare) while print can take one argument. echo is marginally faster than print.
Echo example:
<?php
$txt1 = "Learn PHP";
$txt2 = "vissicomp.com";
$x = 5;
$y = 4;

echo "<h2>$txt1</h2>";
echo "Study PHP at $txt2<br>";
echo $x + $y;
?>
Output:
Learn PHP
Study PHP at vissicomp.com
9

Print Example:

<?php
print "<h2>PHP is Fun!</h2>";
print "Hello world!<br>";
print "I'm about to learn PHP!";
?> 

PHP Data Types :
Variables can store data of different types, and different data types can do different things.
PHP supports the following data types:
  • String
  • Integer
  • Float (floating point numbers - also called double)
  • Boolean
  • Array
  • Object
  • NULL
  • Resource
  •  
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;
?>

Output:

Hello world!
Hello world!

PHP Integer
An integer is a whole number (without decimals).  It is a number between -2,147,483,648 and +2,147,483,647.
Rules for integers:
  • An integer must have at least one digit (0-9)
  • An integer cannot contain comma or blanks
  • 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;
var_dump($x);

?>

Output:
int(5985)

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:
<?php 
$x = 10.365;
var_dump($x);
?>
Output:
float(10.365)

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

?>
Output:
array(3) { [0]=> string(5) "Volvo" [1]=> string(3) "BMW" [2]=> string(6) "Toyota" }

PHP 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

$herbie = new Car();

// show object properties

echo $herbie->model;

?>

Output :
VW

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.
Tip: 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:

Example :
<?php

$x = "Hello world!";

$x = null;

var_dump($x);

?>

Output:

NULL


PHP String Functions :

Get The Length of a String:
The PHP strlen() function returns the length of a string (number of characters).
The example below returns the length of the string "Hello world!":
<?php
echo strlen("Hello world!"); // outputs 12
?>

Output:
12


Count The Number of Words in a String
The PHP str_word_count() function counts the number of words in a string:
<?php

echo str_word_count("Hello world!"); // outputs 2

?>
Output :
2

Reverse a String
The PHP strrev() function reverses a string:
<?php

echo strrev("Hello world!");   // outputs !dlrow olleH

?>
Output:
!dlrow olleH

Search For a Specific Text Within a String
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.
The example below searches for the text "world" in the string "Hello world!":
<?php

echo strpos("Hello world!", "world"); // outputs 6

?>
Output:
6

Replace Text Within a String
The PHP str_replace() function replaces some characters with some other characters in a string.
The example below replaces the text "world" with "Dolly":
<?php

echo str_replace("world", "Dolly", "Hello world!"); // outputs Hello Dolly!

?>

Hello Dolly!






Create a PHP Constant

To create a constant, use the define() function.

Syntax

define(name, value, case-insensitive)
Parameters:
  • name: Specifies the name of the constant
  • value: Specifies the value of the constant
  • case-insensitive: Specifies whether the constant name should be case-insensitive. Default is false
The example below creates a constant with a case-sensitive name:
<?php

define("GREETING", "Welcome to vissicomp.com!");

echo GREETING;

?>

Output:
Welcome to vissicomp.com!


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

PHP Arithmetic Operators:

+
Addition

 

<?php

$x = 10; 
$y = 6;

echo $x + $y;
?>
 
Output:
16

-
Subtraction
<?php
$x = 10; 
$y = 6;

echo $x - $y;
?>
  
Output:
4
*
Multiplication

<?php
$x = 10; 
$y = 6;

echo $x * $y;
?>
  
Output:
60
/
Division

<?php
$x = 10; 
$y = 6;

echo $x / $y;

?>
  
Output:
%
Modulus

<?php
$x = 10; 
$y = 6;

echo $x % $y;
?>
 
Output:
4

PHP Assignment Operators :

The PHP assignment operators are used with numeric values to write a value to a variable.
x = y
<?php
$x = 10; 
echo $x;
?>
 
Output:
10

a += b
a= a + b
Addition

<?php

$a= 20; 

$b=100;
$a +=$b;
echo $a;
?>
 
Output:
120

a -= b
a = a- b
Subtraction


<?php

$a = 50;

$b=30;
$a -= $b;

echo $a;

?>
 
Output:
20

a *= b
a = a* b
Multiplication

<?php

$a = 10; 

$b = 6;
$a*=$b;
echo $a;

?>
 
Output:
60

a/= b
a= a/ b
Division

<?php

$a = 10;

$b=5;
$a /= $b;

echo $a;

?>
 
Output:
2
a %= b
a= a % b
Modulus

<?php
$a = 15;
$b=4;
$a%=$b;
echo $a;
?>
 
Output:
3

PHP Comparison Operators

The PHP comparison operators are used to compare two values (number or string):
==
Equal
$x == $y
Returns true if 
$x is equal to $y

<?php
$x = 100; 
$y = "100";

var_dump($x == $y); // returns true because values are equal

?>
 
Output:
Bool(true)

!=
Not equal
$x != $y
Returns true if $x
 is not equal to $y


<?php
$x = 100; 
$y = "100";

var_dump($x != $y); // returns false because values are equal
?>
 
Output:
Bool(false)

> 
Greater than
$x > $y
Returns true if 
$x is greater than $y
<?php
$x = 100;
$y = 50;

var_dump($x > $y); // returns true because $x is greater than $y
?>
  

Output:
bool(true)

< 
Less than
$x < $y
Returns true if
 $xis less than $y
<?php
$x = 10;
$y = 50;

var_dump($x <  $y); // returns true because $x is less than $y
?>
  
Output:
Bool(true)

>=
Greater than or equal to
$x >= $y
Returns true if
 $x is greater than or equal to $y

<?php

$x = 50;

$y = 50;

var_dump($x >= $y); // returns true because $x is greater than or equal to $y

?>
 
Output:
bool(true)


<=
Less than or equal to
$x <= $y
Returns true if 
$x is less than or equal to $y






<?php
$x = 50;
$y = 50;

var_dump($x <= $y); // returns true because $x is less than or equal to $y
?>
  
Output:
bool(true)


PHP Increment / Decrement Operators

The PHP increment operators are used to increment a variable's value.
The PHP decrement operators are used to decrement a variable's value.
++$x
Pre-increment
Increments $x by one, then returns $x
<?php
$x = 10; 
echo ++$x;
?>
Output:
11

$x++
Post-increment
Returns $x, then increments $x by one

<?php
$x = 10; 
echo $x++;
?>
  
Output:
10
--$x
Pre-decrement
Decrements $x by one, then returns $x
<?php
$x = 10; 
echo --$x;
?>
   
Output:
9
$x--
Post-decrement
Returns $x, then decrements $x by one

<?php
$x = 10; 
echo $x--;
?>
 
Output:
10

PHP Logical Operators

The PHP logical operators are used to combine conditional statements.


&&
And
$x && $y
True if both $x and $y are true

<?php
$x = 100; 
$y = 50;

if ($x == 100 && $y == 50) {
     echo "Hello world!";
}
?>
 
Output:
Hello world!

||
Or
$x || $y
True if either $x or $y is true
<?php

$x = 100; 
$y = 50;

if ($x == 100 || $y == 80) {
     echo "Hello world!";
}
?>
   
Output:

Hello world!

!
Not
!$x
True if $x is not true

<?php

$x = 100; 

if ($x !== 90)
{
     echo "Hello world!";
}

?>
 
Output:
Hello world!

Ternary Logic:

* most basic usage */
$var = 5;
$var_is_greater_than_two = ($var > 2 ? true : false); // returns true

What Are The Advantages of Ternary Logic?

There are some valuable advantages to using this type of logic:
  • Makes coding simple if/else logic quicker
  • You can do your if/else logic inline with output instead of breaking your output building for if/else statements
  • Makes code shorter
Example :

<?php
$a=10;
$b=15;
echo $a>$b?10:20;
?>

output:
20



PHP Array Operators: 
The PHP array operators are used to compare arrays.

Operator
Name
Example
Result
+
Union
$x + $y
Union of $x and $y
==
Equality
$x == $y
Returns true if $x and $y have the same key/value pairs
===
Identity
$x === $y
Returns true if $x and $y have the same key/value pairs in the same order and of the same types
!=
Inequality
$x != $y
Returns true if $x is not equal to $y
<> 
Inequality
$x <> $y
Returns true if $x is not equal to $y
!==
Non-identity
$x !== $y
Returns true if $x is not identical to $y


 

PHP String Operators:

PHP has two operators that are specially designed for strings.

Operator
Name
Example
Result
.
Concatenation
$txt1 . $txt2
Concatenation of $txt1 and $txt2
.=
Concatenation assignment
$txt1 .= $txt2
Appends $txt2 to $txt1






Written by

We are Creative Blogger Theme Wavers which provides user friendly, effective and easy to use themes. Each support has free and providing HD support screen casting.

1 comments:

  1. Thanks for sharing this PHP Data Types and operators information. Useful blogpost. Learn PHP Training in Bangalore

    ReplyDelete

PHP Training Mumbai, PHP MySQL Training Mumbai, PHP Course Mumbai, PHP Institute Mumbai

 

© 2013 PHP TRAINING IN MUMBAI BY OM SIR . All rights resevered. Designed by OM SIR

Back To Top