
|
Casting to NULL
Date posted: Mar 12, 2009 06:47 AM
Posted by: jmagdada
Casting to NULL
Casting a variable to null will remove the variable and unset its value.
1 Comment Leave a Comment
Converting to object
Date posted: Mar 12, 2009 06:46 AM
Posted by: jmagdada Converting to object If an object is converted to an object, it is not modified. If a value of any other type is converted to an object, a new instance of the stdClass built-in class is created. If the value was NULL, the new instance will be empty. Arrays convert to an object with properties named by keys, and corresponding values. For any other value, a member variable named scalar will contain the value. $obj = (object) 'ciao'; echo $obj->scalar; // outputs 'ciao' ?>
0 Comment Leave a Comment
Converting to array
Date posted: Mar 12, 2009 06:45 AM
Posted by: jmagdada
Converting to array
For any of the types: integer, float, string, boolean and resource, converting a value to an array results in an array with a single element with index zero and the value of the scalar which was converted. In other words, (array)$scalarValue is exactly the same as array($scalarValue). If an object is converted to an array, the result is an array whose elements are the object's properties. The keys are the member variable names, with a few notable exceptions: integer properties are unaccessible; private variables have the class name prepended to the variable name; protected variables have a '*' prepended to the variable name. These prepended values have null bytes on either side. This can result in some unexpected behaviour: class A { private $A; // This will become '\0A\0A' } class B extends A { private $A; // This will become '\0B\0A' public $AA; // This will become 'AA' } var_dump((array) new B()); ?> The above will appear to have two keys named 'AA', although one of them is actually named '\0A\0A'. Converting NULL to an array results in an empty array.
0 Comment Leave a Comment
Converting to string
Date posted: Mar 12, 2009 06:44 AM
Posted by: jmagdada
Converting to string
A value can be converted to a string using the (string) cast or the strval() function. String conversion is automatically done in the scope of an expression where a string is needed. This happens when using the echo() or print() functions, or when a variable is compared to a string. The sections on Types and Type Juggling will make the following clearer. See also the settype() function. A boolean TRUE value is converted to the string "1". Boolean FALSE is converted to "" (the empty string). This allows conversion back and forth between boolean and string values. An integer or float is converted to a string representing the number textually (including the exponent part for floats). Floating point numbers can be converted using exponential notation (4.1E+6). Note: The decimal point character is defined in the script's locale (category LC_NUMERIC). See the setlocale() function. Arrays are always converted to the string "Array"; because of this, echo() and print() can not by themselves show the contents of an array. To view a single element, use a construction such as echo $arr['foo']. See below for tips on viewing the entire contents. Objects in PHP 4 are always converted to the string "Object". To print the values of object members for debugging reasons, read the paragraphs below. To get an object's class name, use the get_class() function. As of PHP 5, the __toString method is used when applicable. Resources are always converted to strings with the structure "Resource id #1", where 1 is the unique number assigned to the resource by PHP at runtime. Do not rely upon this structure; it is subject to change. To get a resource's type, use the get_resource_type() function. NULL is always converted to an empty string. As stated above, directly converting an array, object, or resource to a string does not provide any useful information about the value beyond its type. See the functions print_r() and var_dump() for more effective means of inspecting the contents of these types. Most PHP values can also be converted to strings for permanent storage. This method is called serialization, and is performed by the serialize() function. If the PHP engine was built with WDDX support, PHP values can also be serialized as well-formed XML text. String conversion to numbers When a string is evaluated in a numeric context, the resulting value and type are determined as follows. The string will be evaluated as a float if it contains any of the characters '.', 'e', or 'E'. Otherwise, it will be evaluated as an integer. The value is given by the initial portion of the string. If the string starts with valid numeric data, this will be the value used. Otherwise, the value will be 0 (zero). Valid numeric data is an optional sign, followed by one or more digits (optionally containing a decimal point), followed by an optional exponent. The exponent is an 'e' or 'E' followed by one or more digits. $foo = 1 + "10.5"; // $foo is float (11.5) $foo = 1 + "-1.3e3"; // $foo is float (-1299) $foo = 1 + "bob-1.3e3"; // $foo is integer (1) $foo = 1 + "bob3"; // $foo is integer (1) $foo = 1 + "10 Small Pigs"; // $foo is integer (11) $foo = 4 + "10.2 Little Piggies"; // $foo is float (14.2) $foo = "10.0 pigs " + 1; // $foo is float (11) $foo = "10.0 pigs " + 1.0; // $foo is float (11) ?> For more information on this conversion, see the Unix manual page for strtod(3). To test any of the examples in this section, cut and paste the examples and insert the following line to see what's going on: echo "\$foo==$foo; type is " . gettype ($foo) . " \n"; ?> Do not expect to get the code of one character by converting it to integer, as is done in C. Use the ord() and chr() functions to convert between ASCII codes and characters.
0 Comment Leave a Comment
Converting to float
Date posted: Mar 12, 2009 06:42 AM
Posted by: jmagdada
Converting to float
For information on converting strings to float, see String conversion to numbers. For values of other types, the conversion is performed by converting the value to integer first and then to float. See Converting to integer for more information. As of PHP 5, a notice is thrown if an object is converted to float.
0 Comment Leave a Comment
Converting to boolean
Date posted: Mar 12, 2009 06:41 AM
Posted by: jmagdada
Converting to boolean
To explicitly convert a value to boolean, use the (bool) or (boolean) casts. However, in most cases the cast is unncecessary, since a value will be automatically converted if an operator, function or control structure requires a boolean argument. When converting to boolean, the following values are considered FALSE: * the boolean FALSE itself * the integer 0 (zero) * the float 0.0 (zero) * the empty string, and the string "0" * an array with zero elements * an object with zero member variables (PHP 4 only) * the special type NULL (including unset variables) * SimpleXML objects created from empty tags Every other value is considered TRUE (including any resource).
0 Comment Leave a Comment
Converting to integer
Date posted: Mar 12, 2009 06:39 AM
Posted by: jmagdada
Converting to integer
To explicitly convert a value to integer, use either the (int) or (integer) casts. However, in most cases the cast is not needed, since a value will be automatically converted if an operator, function or control structure requires an integer argument. A value can also be converted to integer with the intval() function. From booleans FALSE will yield 0 (zero), and TRUE will yield 1 (one). From floating point numbers When converting from float to integer, the number will be rounded towards zero. If the float is beyond the boundaries of integer (usually +/- 2.15e+9 = 2^31), the result is undefined, since the float doesn't have enough precision to give an exact integer result. No warning, not even a notice will be issued when this happens!
0 Comment Leave a Comment
Multidimensional Array
Date posted: Mar 12, 2009 06:10 AM
Posted by: jmagdada
EXTRACTING A MULTIDIMENSIONAL ARRAY
$shop = array( array("rose", 1.25 , 15),
array("daisy", 0.75 , 25),
array("orchid", 1.15 , 7)
);
$count = count($shop);
for( $a = 0; $a < $count; $a++){
foreach($shop[$a] as $val){
echo $val.' ';
}
echo '
0 Comment Leave a Comment
Differences between constants and variables
Date posted: Mar 12, 2009 05:58 AM
Posted by: jmagdada
These are the differences between constants and variables:
* Constants do not have a dollar sign ($) before them; * Constants may only be defined using the define() function, not by simple assignment; * Constants may be defined and accessed anywhere without regard to variable scoping rules; * Constants may not be redefined or undefined once they have been set; and * Constants may only evaluate to scalar values.
0 Comment Leave a Comment
What are the advantages of using functions?
Date posted: Mar 12, 2009 03:17 AM
Posted by: jmagdada
What are the advantages of using functions?
Ø Debugging is easier Ø It is easier to understand the logic involved in the program Ø Testing is easier Ø Recursive call is possible Ø Irrelevant details in the user point of view are hidden in functions Ø Functions are helpful in generalizing the program
0 Comment Leave a Comment
Difference between single-quote( ' ) and double-quote ( " ) strings in php
Date posted: Mar 12, 2009 03:15 AM
Posted by: jmagdada
Difference between single-quote( ' ) and double-quote ( " ) strings in php
Briefly: In double-quoted strings, variables are replaced by their values: PHP Code: $word = "Hello". echo "$word there!" The above code will output: Hello there! Doing the same with single quotes: PHP Code: $word = "Hello". echo '$word there!' ...will print: $word there! Using single quotes where possible, will be slightly more efficient to process, because PHP doesn't have to search single quoted strings for variables.
0 Comment Leave a Comment
UNDECIDED have new LOGO!
Date posted: Nov 15, 2008 03:24 AM
Posted by: jmagdada
6 Comments Leave a Comment
Leave Comments
Date posted: Nov 11, 2008 08:48 PM
Posted by: jmagdada
![]()
3 Comments Leave a Comment
Jeepney Stop - Chapter 1
Date posted: Nov 11, 2008 08:18 AM
Posted by: jmagdada Gikan kini sa mga imahinasyon/experience ni Roel Villarba. Og pinaagi sa iyang balaang kamot, iya kini gisulat . [Pahibalo: Ang pangalan sa lugar ug sa mga tawo nga nahisgotan diri nga storya kay wala gayud tuyu-a apan usa lamang ka minugna sa nagsuwat.]
3 Comments Leave a Comment
Absolutely Free
Date posted: Oct 28, 2008 10:48 PM
Posted by: jmagdada I would like to tell you about free web hosting service which I used now for Undecided. I think this information can be useful for you. If you plan to get your website, This is one good free web hosting provider to choose - 000webhost.com.
2 Comments Leave a Comment
Under Development and Testing
Date posted: Oct 27, 2008 01:14 AM
Posted by: jmagdada Sigh.. my blog is still under development and testing.. So far, i can now post articles which is the most important of all!.
0 Comment Leave a Comment
Undecided!?
Date posted: Oct 26, 2008 08:25 PM
Posted by: jmagdada
0 Comment Leave a Comment
|
|
|
Recent Articles
Casting to NULL
Date posted: Mar 12, 2009 06:47 AM
Posted by: jmagdada Converting to object
Date posted: Mar 12, 2009 06:46 AM
Posted by: jmagdada Converting to array
Date posted: Mar 12, 2009 06:45 AM
Posted by: jmagdada Converting to string
Date posted: Mar 12, 2009 06:44 AM
Posted by: jmagdada Converting to float
Date posted: Mar 12, 2009 06:42 AM
Posted by: jmagdada Converting to boolean
Date posted: Mar 12, 2009 06:41 AM
Posted by: jmagdada Converting to integer
Date posted: Mar 12, 2009 06:39 AM
Posted by: jmagdada Multidimensional Array
Date posted: Mar 12, 2009 06:10 AM
Posted by: jmagdada |
Categories
|
About Me
I'm jmagdada. Certified tagaBukid. Just graduated this year at Center for Industrial Technology and Enterprise and now currently working as a Junior PHP Programmer at East Asia IT Services. Have been addicted to anime. Yeah anime!, you know, like Naruto,Bleach and Gundam Seed and Destiny! and more.. lol. |