Манипуляции с типами

PHP не требует (и не поддерживает) явного определения типа при объявлении переменной; тип переменной определяется по контексту, в котором она используется. То есть, если вы присвоите строковое значение переменной $var, $var станет строкой. Если вы затем присвоите $var целочисленное значение, она станет целым числом.

Примером автоматического преобразования типа является оператор сложения '+'. Если любой из операндов является числом с плавающей точкой, то все операнды интерпретируются как числа с плавающей точкой, результатом будет также число с плавающей точкой. В противном случае операнды будут интерпретироваться как целые числа и результат также будет целочисленным. Обратите внимание, что это НЕ меняет типы самих операндов; меняется только то, как они вычисляются.

<?php
$foo
= "0"// $foo это строка (ASCII 48)
$foo += 2// $foo теперь целое число (2)
$foo = $foo + 1.3// $foo теперь число с плавающей точкой (3.3)
$foo = 5 + "10 Little Piggies"; // $foo это целое число (15)
$foo = 5 + "10 Small Pigs";    // $foo это целое число (15)
?>

Если последние два примера вам непонятны, смотрите Преобразование строк в числа.

Если вы хотите, чтобы переменная принудительно вычислялась как определенный тип, смотрите раздел приведение типов. Если вы хотите изменить тип переменной, смотрите settype().

Если вы хотите протестировать любой из примеров, приведенных в данном разделе, вы можете использовать функцию var_dump().

Замечание: Поведение автоматического преобразования в массив в настоящий момент не определено.

<?php
$a
= "1";    // $a это строка
$a[0] = "f"// А как же смещение строки? Что произойдет?
?>

Поскольку PHP (по историческим причинам) поддерживает индексирование в строках с использованием такого же синтаксиса, как и при индексировании массива, вышеприведенный пример приводит к проблеме: следует ли $a стать массивом, первым элементом которого будет "f" или "f" должна стать первым символом строки $a?

Текущая версия PHP воспринимает второе присваивание как определение смещения строки, поэтому $a станет "f", результат же этого автоматического преобразования следует, однако, рассматривать как неопределенный. В PHP 4 для доступа к символам строки был введен новый синтаксис фигурных скобок, используйте этот синтаксис вместо вышеприведенного:

<?php
$a   
= "abc"; // $a это строка
$a{1} = "f"// $a теперь содержит "afc"
?>

Для дополнительной информации смотрите раздел Доступ к символу в строке.

Приведение типов

Приведение типов в PHP работает так же, как и в C: имя требуемого типа записывается в круглых скобках перед приводимой переменной.

<?php
$foo
= 10// $foo это целое число
$bar = (boolean) $foo// $bar это булев тип
?>

Допускаются следующие приведения типов:

  • (int), (integer) - приведение к целому числу

  • (bool), (boolean) - приведение к булеву типу

  • (float), (double), (real) - приведение к числу с плавающей точкой (float)

  • (string) - приведение к строке

  • (array) - приведение к массиву

  • (object) - приведение к объекту

Обратите внимание, что внутри скобок допускаются пробелы и символы табуляции, поэтому следующее равносильно по своему действию:

<?php
$foo
= (int) $bar;
$foo = ( int ) $bar;
?>

Замечание: Вместо приведения переменной к строке, вы можете заключить ее в двойные кавычки.

<?php
$foo
= 10;            // $foo это целое число
$str = "$foo";        // $str это строка
$fst = (string) $foo; // $fst это также строка

// Это напечатает "они одинаковы"
if ($fst === $str) {
   echo
"они одинаковы";
}
?>

Возможно, вам не совсем ясно, что происходит при приведении между типами. Для дополнительной информации смотрите разделы:



Манипуляции с типами
frederick_ricaforte at yahoo dot com
18-Jul-2006 12:38
<?php
// I got a string:
$str= "0000";

var_dump($str);
echo
"\\n$str is ".(is_numeric($str)?"numeric":"not numeric");
echo
"\\n$str>0 , ".(($str>0)?"yes":"no");

/** take a note of this **/
echo "\\n$str=='000' , ".(($str=="000")?"yes":"no");
/** endnote **/

echo "\\n$str==0 , ".(($str==0)?"yes":"no");
echo
"\\n$str<0 , ".(($str<0)?"yes":"no");
$au = $str*1;
echo
"\\n$str*1 is equal to ".$au;
echo
"\\n";
var_dump($au);
?>
______________________________
string(4) "0000"

0000 is numeric
0000>0 , no
0000=='000' , yes
0000==0 , yes
0000<0 , no
0000*1 is equal to 0
int(0)
______________________________
0000=='000' , yes
Seems like in type juggling, numeric test has more priority over string comparison.

Frederick Ricaforte
jsingh dot oberoi at gmail dot com
13-May-2006 03:23
Reply to note dated 25-May-2005 05:35 :

In PHP 5.1.4, cast only affects the destination type and not the source type.

So,

<?php

$a
= "abc";
$bb = (int)$a;
echo
$a;

?>

gives as output:
abc
miracle at 1oo-percent dot de
20-Feb-2006 05:26
If you want to convert a string automatically to float or integer (e.g. "0.234" to float and "123" to int), simply add 0 to the string - PHP will do the rest.

e.g.

$val = 0 + "1.234";
(type of $val is float now)

$val = 0 + "123";
(type of $val is integer now)
22-Jun-2005 05:47
If you have a boolean, performing increments on it won't do anything despite it being 1.  This is a case where you have to use a cast.

<html>
<body> <!-- don't want w3.org to get mad... -->
<?php
$bar
= TRUE;
?>
I have <?=$bar?> bar.
<?php
$bar
++;
?>
I now have <?=$bar?> bar.
<?php
$bar
= (int) $bar;
$bar++;
?>
I finally have <?=$bar?> bar.
</body>
</html>

That will print

I have 1 bar.
I now have 1 bar.
I finally have 2 bar.
25-May-2005 10:35
It appears in PHP 5.0.0, that casting will convert the variable to the specified type, not return a copy of the variable converted to that type.

ie:

$str = "foo";
$int = (int) $str;
echo "str: $str, int: $int";
// prints out "0, 0" instead of "foo, 0".

How it works with classes, I don't know.
toma at smartsemantics dot com
09-Mar-2005 06:24
In my much of my coding I have found it necessary to type-cast between objects of different class types.

More specifically, I often want to take information from a database, convert it into the class it was before it was inserted, then have the ability to call its class functions as well.

The following code is much shorter than some of the previous examples and seems to suit my purposes.  It also makes use of some regular expression matching rather than string position, replacing, etc.  It takes an object ($obj) of any type and casts it to an new type ($class_type).  Note that the new class type must exist:

function ClassTypeCast(&$obj,$class_type){
   if(class_exists($class_type,true)){
       $obj = unserialize(preg_replace"/^O:[0-9]+:\"[^\"]+\":/i",
         "O:".strlen($class_type).":\"".$class_type."\":", serialize($obj)));
   }
}
Raja
10-Feb-2005 03:05
Uneven division of an integer variable by another integer variable will result in a float by automatic conversion -- you do not have to cast the variables to floats in order to avoid integer truncation (as you would in C, for example):

$dividend = 2;
$divisor = 3;
$quotient = $dividend/$divisor;
print $quotient; // 0.66666666666667
memandeemail at gmail dot com
09-Dec-2004 05:29
/**
   * @return bool
   * @param array[byreference] $values
   * @desc Convert an array or any value to Escalar Object [not tested in large scale]
   */
   function setobject(&$values) {
       $values = (object) $values;
       foreach ($values as $tkey => $val) {
           if (is_array($val)) {
               setobject($val);
               $values->$tkey = $val;
           }
       }
       return (bool) $values;
   }
tom5025_ at hotmail dot com
24-Aug-2004 01:27
function strhex($string)
{
   $hex="";
   for ($i=0;$i<strlen($string);$i++)
       $hex.=dechex(ord($string[$i]));
   return $hex;
}
function hexstr($hex)
{
   $string="";
   for ($i=0;$i<strlen($hex)-1;$i+=2)
       $string.=chr(hexdec($hex[$i].$hex[$i+1]));
   return $string;
}

to convert hex to str and vice versa
dimo dot vanchev at bianor dot com
10-Mar-2004 07:02
For some reason the code-fix posted by philip_snyder at hotmail dot com [27-Feb-2004 02:08]
didn't work for me neither with long_class_names nor with short_class_names. I'm using PHP v4.3.5 for Linux.
Anyway here's what I wrote to solve the long_named_classes problem:

<?php
function typecast($old_object, $new_classname) {
   if(
class_exists($new_classname)) {
      
$old_serialized_object = serialize($old_object);
      
$old_object_name_length = strlen(get_class($old_object));
      
$subtring_offset = $old_object_name_length + strlen($old_object_name_length) + 6;
      
$new_serialized_object  = 'O:' . strlen($new_classname) . ':"' . $new_classname . '":';
      
$new_serialized_object .= substr($old_serialized_object, $subtring_offset);
       return
unserialize($new_serialized_object);
     } else {
         return
false;
     }
}
?>
philip_snyder at hotmail dot com
27-Feb-2004 07:08
Re: the typecasting between classes post below... fantastic, but slightly flawed. Any class name longer than 9 characters becomes a problem... SO here's a simple fix:

function typecast($old_object, $new_classname) {
  if(class_exists($new_classname)) {
   // Example serialized object segment
   // O:5:"field":9:{s:5:...  <--- Class: Field
   $old_serialized_prefix  = "O:".strlen(get_class($old_object));
   $old_serialized_prefix .= ":\"".get_class($old_object)."\":";

   $old_serialized_object = serialize($old_object);
   $new_serialized_object = 'O:'.strlen($new_classname).':"'.$new_classname . '":';
   $new_serialized_object .= substr($old_serialized_object,strlen($old_serialized_prefix));
   return unserialize($new_serialized_object);
  }
  else
   return false;
}

Thanks for the previous code. Set me in the right direction to solving my typecasting problem. ;)
post_at_henribeige_dot_de
03-May-2003 09:37
If you want to do not only typecasting between basic data types but between classes, try this function. It converts any class into another. All variables that equal name in both classes will be copied.

function typecast($old_object, $new_classname) {
  if(class_exists($new_classname)) {
   $old_serialized_object = serialize($old_object);
   $new_serialized_object = 'O:' . strlen($new_classname) . ':"' . $new_classname . '":' .
                             substr($old_serialized_object, $old_serialized_object[2] + 7);
   return unserialize($new_serialized_object);
  }
  else
   return false;
}

Example:

class A {
  var $secret;
  function A($secret) {$this->secret = $secret;}
  function output() {echo("Secret class A: " . $this->secret);}
}

class B extends A {
  var $secret;
  function output() {echo("Secret class B: " . strrev($this->secret));}
}

$a = new A("Paranoia");
$b = typecast($a, "B");

$a->output();
$b->output();
echo("Classname \$a: " . get_class($a) . "Classname \$b: " . get_class($b));

Output of the example code above:

Secret class A: Paranoia
Secret class B: aionaraP
Classname $a: a
Classname $b: b
yury at krasu dot ru
27-Nov-2002 01:24
incremental operator ("++") doesn't make type conversion from boolean to int, and if an variable is boolean and equals TRUE than after ++ operation it remains as TRUE, so:

$a = TRUE;
echo ($a++).$a;  // prints "11"
28-Aug-2002 10:26
Printing or echoing a FALSE boolean value or a NULL value results in an empty string:
(string)TRUE //returns "1"
(string)FALSE //returns ""
echo TRUE; //prints "1"
echo FALSE; //prints nothing!
amittai at NOSPAMamittai dot com
20-Aug-2002 11:30
Uneven division of an integer variable by another integer variable will result in a float by automatic conversion -- you do not have to cast the variables to floats in order to avoid integer truncation (as you would in C, for example):

$dividend = 2;
$divisor = 3;
$quotient = $dividend/$divisor;
print $quotient; // 0.66666666666667

Amittai Aviram

<Псевдо-типы, используемые в этой документацииПеременные>
 Last updated: Tue, 15 Nov 2005