Monday, September 21, 2009

PHP - Type Juggling & Type Casting

 PHP - Type Juggling & Type Casting 


( Int *  float  =  float )
Ex :
php
              $a = 5;
              $b = 12.7;
              $c = $a+$b;
              echo $c;
             
?>
-----------------------------------------------------------------------
PHP - Type Juggling &Type Casting
It is used to convert one data type to another data type Except  Array and Object .
php
              $a = 10;
              $b = (string)$a;
              var_dump($b);
              echo '
';

              var_dump($a);
             
?>

Read  php Interview Questions and Answers by experts at www.tipsoninterview.com

For Php Training in Hyderabad call 9394799566

PHP - Variable functions

PHP -Variable functions

Gettype()
Settype()
Isset()
Empty()
Unset()
Is_...()
Intval()
Example for gettype()
It is used to get the data type
Var_dump() is used to get datatype,length and value but it return only datatype
php
            $a = 5;
            $b = gettype($a);
            echo $b;
           
?>
Example for settype()
1)It is used to set the data type to a variable
2) if it is set successful then it returns true (1) else it return false (null)
php
            $a = 5;
            $b = settype($a,'string');
            var_dump($b);
            echo gettype($a);
?>
Example for isset()
If variable is existed it returns true(1) else it return false (null)
php
            $a = 10;
            $b = isset($a);
            var_dump($b);
?>
Example for empty()
If variable is empty the it will return true (1) else it return false (null)
php
            $a = 0;
            $b = empty($a);
            var_dump($b);
?>

Empty means :
False
0
Empty string
Empty array
Empty object
Null
Not existed
Non empty means :
True
 -infinity to + infinity  ( Except 0 )
Non empty string
Non empty array
Non empty object
Example for unset()
  1. It is not return any value.
  2. It will remove allotted memory to the variable  (it deletes the variable)
 php
            $a = 10;
            unset($a);
            $b = empty($a);
            var_dump($b);
?>
Example for is_..()
it will return true (1) else it return false (null)
is_integer(var)
ex 1:
php
            $a = 10;
            $b = is_integer($a);
            var_dump($b);
?>
-----------------------------------------------------------------------
ex 2:
php
            $a = '10';
            $b = is_integer($a);
            var_dump($b);
?>
-----------------------------------------------------------------------
is_bool(var)
is_string(var)
is_array(var)
is_object(var)
Example for intval()
  1. when you performed arithmetic operation then “ string conversion method “ will work
  2. But with out performed arithmetic operation , if you want to convert a value into int then intval() is useful
 Ex 1 :
php
            $a = '12abc';
            $b = intval($a);
            echo $b;
?>
Ex 2 :
php
            $a = 'abc';
            $b = intval($a);
            echo $b;
?>


Read  php Interview Questions and Answers by experts at www.tipsoninterview.com

For Php Training in Hyderabad call 9394799566

php - Operators

php - Operators

Arithmetic
Relational
Logical
Concatenation
Assignment
Inc / Dec
Bitwise Operator
Ternary

Arithmetic
            +  Addition
            -   Subtraction
            *   Product
            /    Division
            %  Modules        (5%2 = 1)

      $a = $a + $b
    $a + = $b
      $a = $a - $b
    $a -  = $b
      $a = $a * $b
    $a * = $b
     $a = $a / $b
    $a / = $b
     $a = $a % $b
    $a % = $b

Relational
==     Equals to
===  Identical                       ( It will check value and data type )
!=      Not equals to
!==   Not identical
>       Greater then
<       Less then
>=     Greater then or Equals to
<=     Less then or Equals to
Example 1
php
                   $a = 10;
                   $b = '10abc';
                   if($a == $b)
                             echo 'Pass';
                   else
                             echo 'fail';
?>
Example 2
php
                   $a = 10;
                   $b = '10abc';
                   if($a === $b)
                             echo 'Pass';
                   else
                             echo 'fail';
?>
Logical
And            &&
Or              ||
Not             !
Example
php
                   $a = 5;
                   $b = 20;
                   if($a>2 && $b>15)
                             echo 'Pass';
                   else
                             echo 'fail';
?>
Concatenation
.  or  ,                  
Assignment
$a = 10 ;
$a = $b ;
Incrementation/ Decrementation
                      ++              incrementation
                      --                Decrementation
Ex :
                     
                      $a = 10 ;
                      $a++;   similar to ( $a + = 1)  that is    $a = $a + 1
                      $a-- ;    similar to ( $a - = 1)  that is    $a = $a - 1
            $a = 10 ;
  Post increment
            echo ($a++) ;     -        10;
            echo ($a) ;          -        11;
  Pre increment
            echo (++$a) ;     -        11;
            echo ($a) ;          -        11;
Note :
Unari Operator takes one Value or variable .   (  !  ,  ++  ,  --  )
Binary Operator takes two Values or variables .  
Bitwise Operator
 
And            &
Or              |
Xor            ^
Example1
php
                   $a = 12;
                   $b = 9;
                   echo $a & $b;
?>
                  
Output :  8
Note : 
binary value  -  1100
binary value  -  1001
12  &  9         -  1000   -> decimal value 8   ( 1*23  + 0*22  + 0*21 + 0*20 )
            
            T  &  T  = T                           T  |  T  = T             T  ^  T  = F  (both are same F)
            T  &  F  = F                           T  |  F  = T             T  ^  F  = T  (both are diffarent T)
            F  &  T  = F                           F  |  T  = T             F  ^  T  = T
            F  &  F  = F                           F  |  F  = F             F  ^  F  = F
--------------------------------------------------------------------
Example2
php
                   $a = 12;
                   $b = 9;
                   echo $a | $b;
?>
                     
Output :  13
--------------------------------------------------------------------
Example3
php
                   $a = 12;
                   $b = 9;
                   echo $a ^ $b;
?>
                     
Output :  5
--------------------------------------------------------------------
Ternary
Var = ( condition ) ?   val 1  :   val 2
Example
php
           $a = 10;
           $b = 20;
           $c = ( $a > $b ) ? $a : $b;
          
           echo ( $c );
?>

for php training in Hyderabad call 9394799566 

PHP - Constant

Constant


The define() function defines a constant.
Constants are much like variables,

except for the following differences:

A constant's value cannot be changed after it is set
Constant names do not need a leading dollar sign ($)
Constants can be accessed regardless of scope
Constant values can only be strings and numbers

Syntax

define(name,value,case_insensitive)

Ex 1 :



Ex 2 :



Ex 3 :



Ex 4 :



Ex 5 :



Note :

The PHP constant is very similar to a PHP variable in that it is used to store a value but, unlike a variable, the value cannot be changed. It is called a constant because; you guessed it, it stays constant throughout the PHP program. By using a constant you "lock in" the value which prevents you from accidentally changing it.

Secondly, if you want to run a program several times using a different value each time, you do not need to search throughout the entire program and change the value at each instance. You only need to change it at the beginning of the program where you set the initial value for the constant.

Let's take a look at an example where we use the define function to set the initial value of a constant to the state in the US that a business is located in. Then we can use this constant in an accounting program.

define("STATE", "Ohio");

echo STATE;

In the example above, the define function begins with the function name define followed by parenthesis. Within the parenthesis are the name of the constant and the value to be assigned to the constant. Both are enclosed within quotation marks and separated by a comma. This is all followed by a semicolon.
You will notice that the value of Ohio is placed within quotation marks. This is because it is a character string. To store a numeric value you do not need quotation marks.
define("TAX", 4);
You will also notice that, unlike PHP variables, there is no $ in front of the constant name.
Although it is not required, most programmers use all uppercase letters when naming constants to make them stand out from the variables in their program.
You do not use quotation marks when you use a constant in an echo statement. If you do, the echo statement will display the constant name.
echo STATE; // will display Ohio

echo "STATE"; // will display STATE

PHP Introduction

PHP Stands For Pre Processor of Hypertext (Or) Hypertext pre Processor

PHP Definition : It is embedded server side scripting language.

Features of PHP

1. Open Source
2. Natural way of Accessing Data Base (No need of any Drivers)
3. Syntax and semantic is simple
4. Loosely Typed
5. Ability to Connect Multiple Databases
6. Full support in the form of built in functions
(No need to include any Header files to use any built in Functions in PHP)

To install PHP we have some third party tools

EX :

1. XAMPP
2. WAMP
3. Easy PHP

Note :

Our s/w is XAMPP , it includes Apache , My SQL , PHP , Perl for XP

Every server has some root Directory ( where we have to save the programs )

XAMPP - htdocs

WAMP - www

Easy PHP - www
-------------------------------------------------------------

All web servers are deped on Port 80

We can change the port no in :

XAMPP\apache\conf\httpd.conf

-------------------------------------------------------------

Create your folder in

C:\xampp\htdocs to save your programs ( ex : sampath)

-------------------------------------------------------------
Ex 1 :



Save the file in your folder sampath ( a.php )

Run the program :

Go to browser

http://localhost/sampath

select your program from the list to run it

Note :

1) if you save your PHP program with the name index.php in your folder sampath
the the program will automatically run when you are enter http://localhost/sampath in the browser

2) instead of giving localhost we can give like this also http://127.0.0.1/sampath

-------------------------------------------------------------

Variable


Variable is a memory location where we can store the values.

Ex 1 :
Var $a;
$a = ‘srihitha’; - Wrong

Ex 2 :
Var $a = ‘srihitha’; - Wrong

Ex 3 :
$a = ‘srihitha’; - correct

Conditions for variable names :

1. First char of var name must be in A-Z (or) a-z (or) _ (underscore) .
2. Spaces are not allowed.
3. Avoid built in functions and reserve words as variable names.

Data Types :

Numaric ( int – 4 bytes , float – 8 bytes )

String ‘abc’ or “abc”

Boolean True or False

Note :

If any char create problem in a string then use \

Ex :
“Hai \” I am sampath “

\ indicates to browser that coming char is not a special char.

Ex 1 :



Ex 2 :



Note : with using string conversion method it will convert string into integer when u Preformed arithmetic operation

Ex 3 :



Ex 4:



Boolean

Boolean a = true;
Boolean a = false;

Note : don’t put any quotations ( Ex : ‘treue’ )

Ex :



----------------------------------------------------------------

To get the data type of var

Ex 1 :



It will display data type and value.

Ex 2 :



It will display data type,length and value.

Ex 3 :


It will display data type and value.

Note :

“ “ are used for variable expansion

Ex :



-------------------------------------------------------------

Concatenation symbol is . or ,

For next line we can use ‘


-------------------------------------------------------------

Ex :

Php Material

Intrested in learning php then read this collection of trainers material to be expert in php language. For more update in php material and interview questions visit http://www.tipsoninterview.com/php.html