Home arrow Tutorials arrow PHP - Variable Types

Web Development

Welcome to Web Development section. Please choose a category. 
 
  • Hrvatski tutorijali
    Ovdje su objavljeni razni tutorijali na hrvatskom jeziku na temu PC, programiranje, web development, savjeti, trikovi, optreba racunala itd.
  • Tips / Tricks
    Articles about various tips and tricks concerning Windows, Linux and other OS usage.
  • JavaScripts
    Code snippets, tutorials and tricks for JavaScrip language.
  • PHP & MySQL
    Tutorials, code examples, tips and tricks for PHP and MySQL development.
  • Tutorials
    Place for tutorials about using various application in best and simplest way.
PHP - Variable Types Print E-mail
(0 votes)

PHP - Variable Types
Sunday, 10 June 2007
Variable Types

PHP supports eight primitive types.

Four scalar types:

  • boolean : expresses truth value, TRUE or FALSE. Any non zero values and non empty string are also counted as TRUE.
  • integer : round numbers (-5, 0, 123, 555, ...)
  • float : floating-point number or 'double' (0.9283838, 23.0, ...)
  • string : "Hello World", 'PHP and MySQL, etc


Two compound types:

  • array
  • object

And finally two special types:

  • resource ( one example is the return value of mysql_connect() function)
  • NULL

 

In PHP an array can have numeric key, associative key or both. The value of an array can be of any type. To create an array use the array() language construct like this.

<?php
$numbers = array(1, 2, 3, 4, 5, 6);
$age = array("mom" => 45, "pop" => 50, "bro" => 25);
$mixed = array("hello" => "World", 2 => "It's two";

echo "numbers[4] = {$numbers[4]} <br>";
echo "My mom's age is {$age['mom']} <br>";
echo "mixed['hello'] = {$mixed['hello']} <br>";
echo "mixed[2] = {$mixed[2'}";
?>

When working with arrays there is one function I often used. The print_r() function. Given an array this function will print the values in a format that shows keys and elements

<?php
$myarray = array(1, 2, 3, 4, 5);
$myarray[5] = array("Hi", "Hello", "Konnichiwa", "Apa Kabar");

echo '<pre>';
print_r($myarray);
echo '</pre>';
?>

Don't forget to print the preformatting tag <pre> and </pre> before and after calling print_r(). If you don't use them then you'll have to view the page source to see a result in correct format.

Type Juggling

In PHP you don't need to explicitly specify a type for variables. A variable's type is determined by the context in which that variable is used. That is to say, if you assign a string value to variable $var, $var becomes a string. If you then assign an integer value to $var, it becomes an integer.

An example of PHP's automatic type conversion is the addition operator '+'. If any of the operands is a float, then all operands are evaluated as floats, and the result will be a float. Otherwise, the operands will be interpreted as integers, and the result will also be an integer. Note that this does NOT change the types of the operands themselves; the only change is in how the operands are evaluated.

Example :

<?php
$myvar = "0"; // $myvar is string (ASCII 48)
$myvar += 2; // $myvar is now an integer (2)
$myvar = $foo + 1.3; // $myvar is now a float (3.3)
$myvar = 5 + "10 Piglets"; // $foo is integer (15)
?>

Type Casting

To cast a variable write the name of the desired type in parentheses before the variable which is to be cast.


 

<?php
$abc = 10; // $abc is an integer
$xyz = (boolean) $abc; // $xyz is a boolean

echo "abc is $abc and xyz is $xyz <br>";
?>

The casts allowed are:
  • (int), (integer) - cast to integer
  • (bool), (boolean) - cast to boolean
  • (float), (double), (real) - cast to float
  • (string) - cast to string
  • (array) - cast to array
  • (object) - cast to object
 
< Prev   Next >
What's your favorite Internet browser?
 

Login






Lost Password?
No account yet? Register

Tools

Free Link Exchange

Partners

Syndicate