testFunction( (string) $variable); - What is this called? - php

I've seen some code written like this, and I'm really curious what it does and what it's for. Sorry for the unclear title, I appreciate all answers!
Edit: In particular I'm curious about the (string) $variable part

It's called type casting
Type casting in PHP works much as it does in C: the name of the desired type is written in parentheses before the variable which is to be cast.
<?php
$foo = 10; // $foo is an integer
$bar = (boolean) $foo; // $bar is a boolean
?>
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
(unset) - cast to NULL (PHP 5)
In your specific example a variable was being cast to a string before being passed as a parameter to testFunction()

It is a function call with an argument. In this case, a variable $variable has been cast to a string for the argument.

Related

Automatic casting with integer function parameter

With PHP 7 I just found out that if I have a function like this:
function automatic_int_conversion(int $value) {
echo gettype($value) . ' ' . $value;
}
and I call it with a float parameter like this one:
automatic_int_conversion(2.0); # prints out: integer 2
the float(2.0) value is automatically converted to an int, while if I call the function with the float(NAN) this way:
automatic_int_conversion(NAN); # TypeError
I get a type error although the documentation says:
As of PHP 7.0.0, instead of being undefined and platform-dependent, NaN and Infinity will always be zero when cast to integer.
IMHO this is pretty confusing and not consistent, because either every float is automatically converted or none.
Am I missing anything?
There is a difference between type casting (explicit) and type coercion (implicit). The parameter value is not being cast to an int, it is being coerced. You can cast any value to int, e.g.:
echo (int) "a"; // prints 0
But passing "a" to your type declared function throws an error, just as passing NAN does:
automatic_int_conversion("a"); // TypeError
Coercion only works the same as casting on a very limited set of values.
You might want to enable strict types to disable the coercions for function parameters, then it will work 'as expected':
declare(strict_types=1);

Magic function to cast object to integer in php 7

I like to program object oriented so I want to make my own IntegerObject class. But when I try to execute the following code:
$x = new IntegerObject(3)
echo $x / 3;
I get the error:
Object of class IntegerObject could not be converted to int
Is there a magic function like __toString() to cast an object to an integer?
Workaround
As PHP is dynamically type casting language you can magically cast to string and the PHP will cast it to integer:
$x = new IntegerObject(3);
echo "$x" / 3;
You can cast to an integer using the (int) operator:
$x = new IntegerObject(3);
var_dump((int) "$x" / 3);
That should show you that the result is an int.

What does this PHP syntax mean? Can't find any information in the docs

function get( $str ){
$matches = $this->xml->xpath("/conf/item [#name= \"$str\"]");
if (count($matches)) {
$this->lastmatch = $matches[0];
return (string) $matches[0];
}
I can't find any information about what the last line of this code does.
What does this (string) $matches[0] piece do?
As I can guess, it returns the zero element of the array as a string. But I didn't find any mention about this syntax in the docs.
Am I right?
And it will be great if you provide me with a link where I can read about this.
You can read about it here: http://php.net/manual/en/language.types.type-juggling.php
It is called type juggling, because php does not have explicit types.
You can use the following type castings:
(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
(unset) - cast to NULL (PHP 5)

What does (int) $_GET['page'] mean in PHP?

I tried looking up (int) but could only find documentation for the function int() in the PHP manual.
Could someone explain to me what the above code does, and exactly how it works?
You can find it in the manual in the section type juggling: type casting. (int) casts a value to int and is a language construct, which is the reason that it looks "funny".
It convert (tries at least) whatever the value of the variable is to a integer. If there are any letter etc, in front it will convert to a 0.
<?php
$var = '1a';
echo $var; // 1a
echo (int) $var; //1
$var2 = 'a2';
echo $var2; //a2
echo (int) $var2; // 0
?>
(int) converts a value to an integer.
<?php
$test = "1";
echo gettype((int)$test);
?>
$ php test.php
integer
Simple example will make you understand:
var_dump((int)8);
var_dump((int)"8");
var_dump((int)"6a6");
var_dump((int)"a6");
var_dump((int)8.9);
var_dump((int)"8.9");
var_dump((int)"6.4a6");
Result:
int(8)
int(8)
int(6)
int(0)
int(8)
int(8)
int(6)
In PHP, (int) will cast the value following it to an int.
Example:
php > var_dump((int) "5");
int(5)
I believe the syntax was borrowed from C.
What you are looking at there is known as type casting - for more information, see the manual page on type juggling.
The above piece of code casts (or converts) $_GET['page'] to an integer.
this kind of syntax (int) is called type casting. Basically it takes the variable following it and tries to force it into being an int
(int) is same as int()
see
http://php.net/manual/en/language.types.integer.php
it casts the variable following it to integer. more info from documentation:
http://php.net/manual/en/language.types.type-juggling.php
Type casting in PHP works much as it does in C: the name of the
desired type is written in parentheses before the variable which is to
be cast.
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
(unset) - cast to NULL

Using settype in PHP instead of typecasting using brackets, What is the difference?

In PHP you can typecast something as an object like this; (object) or you can use settype($var, "object") - but my question is what is the difference between the two?
Which one is more efficient / better to use? At the moment I find using (object) does the job, but wondering why there is a settype function as well.
Casting changes what the variable is being treated as in the current context, settype changes it permanently.
$value = "100"; //Value is a string
echo 5 + (int)$value; //Value is treated like an integer for this line
settype($value,'int'); //Value is now an integer
Basically settype is a shortcut for:
$value = (type)$value;
settype() alters the actual variable it was passed, the parenthetical casting does not.
If you use settype on $var to change it to an integer, it will permanently lose the decimal portion:
$var = 1.2;
settype($var, "integer");
echo $var; // prints 1, because $var is now an integer, not a float
If you just do a cast, the original variable is unchanged.
$var = 1.2;
$var2 = (integer) $var;
echo $var; // prints 1.2, because $var didn't change type and is still a float
echo $var2; // prints 1
It's worth mentioning that settype does NOT change the variable type permanently. The next time you set the value of the variable, PHP will change its type as well.
$value = "100"; //Value is a string
echo 5 + (int)$value; //Value is treated like an integer for this line
settype($value,'int'); //Value is now an integer
$value = "Hello World"; //Now value is a string
$value = 7; // Now value is an integer
Type Juggling can be frustrating but if you understand what's happening and know your options it can be managed. Use var_dump to get the variables type and other useful info.

Categories