PHP Basic - Casting, Variable, Constants, Error Controlling Operator

7/22/13

After reading these post : PHP basic 1 and 2, i'd like to continue to add more basic on PHP. This time i will adding some basic on casting, variable, constants, and operator.

Casting
PHP can cast data automatically, for example :

<?php
$a = 15;
$b = '2';
$times = $a*$b;
echo $times;
?>
As you see $a is integer and $b is string, but PHP change the string into integer automatically, because the string also number. It can happen only if the inside string is integer type.

Moreover, we can manually casting the variable, for example:

<?php
$a = 15;
$b = 4;
$times = $a/$b;
$casttimes = (int) $times;
echo "before cast : $times<br>";
echo "after cast to integer :$casttimes";
?>
Try to run it, $times will display 3.75 which is float type, and after cast to integer, it will display 3 only. We can cast into data types that available in PHP, which is :
- (int) (integer) : cast into integer
- (float) (double) : cast into float
- (string) : cast into string
- (object) : cast into object
- (array) : cast into array

Variable
- Predefined Variable
We have learned about variable at last post, well there are some predefined variable, that provided by PHP :  

* $_SERVER
To access variable that available in server
* $_POST
To take data from form or HTTP POST
* $_GET
To take data from url
* $_COOKIE
To take data from cookies that stored in web
* $_FILES
To take data from HTTP POST files.

We will use mostly $_POST when we creating web. It's useful when used with form.

- Variable Variable
What's this? sounds weird huh? Yap, this is one rule that apply too in PHP. To understand more, let's take a look at this example :
<?php
$a = 'Alex';
$$a = 'Oxlade';
$$$a = 'Chamberlain';
echo $a."<br>";
echo $$a."<br>";
echo $$$a."<br>";
?>
As you see in code above,  $$a is same as $alex, which is, $alex is still null, and we fill it with 'Oxlade', and when we want to display it, just type $$a, and it will display Oxlade. Same implements that work in $$$a.


Constants
In PHP we can define unit, that can't change during the code implementation. It's unlike variable, which can change during the implementation of program. For ex :

<?php
define ("Pi",3.14);
$r = 10;
$wide = Pi*$r*$r;
echo "Wide = ".$wide;
?>
We define the unit, by define ("Unit name","unit value"), we can define integer, string, and other data type.

Error Controlling Operator (@)
We have learned about operator in previous post, but i forget something, Error Controlling. Yes, this post it to prevent, error syntax that can happen in the web, and change it into something that we define. To understand more, let's take a look at this example :
<?php $host = "localhost"; $user = "root"; $pass = "root"; $conn = @mysql_connect($host,$user,$pass) or die ("Something Wrong"); ?>
The output must be : "Something wrong". Well if we remove the "@", it will display like this picture :

Error Controlling Operator Testing
Error Controlling Operator Testing
So it will be useful to hide some 'robotic type' error message and we create our own version of error message.

That's all for now, the basic. Hope it helps you!

No comments:

 

Tags