преобразовать в целое число php

Преобразовать в целое число php

Casting objects to arrays is a pain. Example:

$test = new MyClass ();
echo ‘

/*
Array
(
[MyClasspriv] => priv_value
[*prot] => prot_value
[pub] => pub_value
[MyClasspriv] => second_pub_value
)
*/

?>

Yes, that looks like an array with two keys with the same name and it looks like the protected field was prepended with an asterisk. But that’s not true:

/*
MyClasspriv (13) => priv_value
0 77 121 67 108 97 115 115 0 112 114 105 118
*prot (7) => prot_value
0 42 0 112 114 111 116
pub (3) => pub_value
112 117 98
MyClasspriv (11) => second_pub_value
77 121 67 108 97 115 115 112 114 105 118
*/

?>

The char codes show that the protected keys are prepended with ‘\0*\0’ and private keys are prepended with ‘\0’.__CLASS__.’\0′ so be careful when playing around with this.

The object casting methods presented here do not take into account the class hierarchy of the class you’re trying to cast your object into.

Value of uninitialized variable of different data types.

settype($a,’bool’);
var_dump($a); //boolean false

settype($b,’string’);
var_dump($b); //string » (length=0)

settype($c,’array’);
var_dump($c); //array (size=0) empty

settype($d,’int’);
var_dump($d); //int 0

settype($e,’float’);
var_dump($e); //float 0

You REALLY must be aware what you are doing, when you cast a lot in your code. For example, you can accidentaly change FALSE to TRUE (probably not in one line, like here):

if(TRUE === (boolean) (array) (int) FALSE) <
kaboom();
>

namaroulis stated «I found it tricky to check if a posted value was an integer»; to test if a variable is a number or a numeric string (such as form input, which is always a string), you must use is_numeric():

in response to bhsmither at gmail.com

It raises a warning because of the bad enquoted variable

It seems (unset) is pretty useless. But for people who like to make their code really compact (and probably unreadable). You can use it to use an variable and unset it on the same line:

?>

With the unset cast:

?>

Hoorah, we lost another line!

Checking for strings to be integers?
How about if a string is a float?

/* When checking for floats, we assume the possibility of no decimals needed. If you MUST require decimals (forcing the user to type 7.0 for example) replace the sequence:
8+(\.7+)?
with
6+\.7+
*/

Источник

Добавить комментарий

Ваш адрес email не будет опубликован. Обязательные поля помечены *