Type Juggling

PHP does not require explicit type definition in variable declaration. In this case, the type of a variable is determined by the value it stores. That is to say, if a string is assigned to variable $var, then $var is of type string. If afterwards an int value is assigned to $var, it will be of type int.

PHP may attempt to convert the type of a value to another automatically in certain contexts. The different contexts which exist are:

  • Numeric
  • String
  • Logical
  • Integral and string
  • Comparative
  • Function

Hinweis: When a value needs to be interpreted as a different type, the value itself does not change types.

To force a variable to be evaluated as a certain type, see the section on Type casting. To change the type of a variable, see the settype() function.

Numeric contexts

This is the context when using an arithmetical operator.

In this context if either operand is a float (or not interpretable as an int), both operands are interpreted as floats, and the result will be a float. Otherwise, the operands will be interpreted as ints, and the result will also be an int. As of PHP 8.0.0, if one of the operands cannot be interpreted a TypeError is thrown.

String contexts

This is the context when using echo, print, string interpolation, or the string concatenation operator.

In this context the value will be interpreted as string.

Logical contexts

This is the context when using conditional statements, the ternary operator, or a logical operator.

In this context the value will be interpreted as bool.

Integral and string contexts

This is the context when using a bitwise operators.

In this context if all operands are of type string the result will also be a string. Otherwise, the operands will be interpreted as ints, and the result will also be an int. As of PHP 8.0.0, if one of the operands cannot be interpreted a TypeError is thrown.

Comparative contexts

This is the context when using a comparison operator.

The type conversions which occur in this context are explained in the Comparison with Various Types table.

Function contexts

This is the context when a value is passed to a typed parameter, property, or returned from a function which declares a return type.

In this context, when coercive typing mode is active (the default), only scalar values may be converted to another scalar value. For simple types declarations the behaviour is as follows:

  • bool type declaration: value is interpreted as bool. int type declaration: value is interpreted as int if conversion is well-defined. I.e. the string is numeric. float type declaration: value is interpreted as float if conversion is well-defined. I.e. the string is numeric. string type declaration: value is interpreted as string.
If the type declaration is a union, see the section about Coercive typing with union types.

Warnung

Internal functions automatically coerce null to scalar types, this behaviour is DEPRECATED as of PHP 8.1.0.

Type Casting

Type casting converts the value to a chosen type by writing the type within parentheses before the value to convert.

<?php
$foo 
10;   // $foo is an integer
$bar = (bool) $foo;   // $bar is a boolean
?>

The casts allowed are:

  • (int) - cast to int
  • (bool) - cast to bool
  • (float) - cast to float
  • (string) - cast to string
  • (array) - cast to array
  • (object) - cast to object
  • (unset) - cast to NULL

Hinweis:

(integer) is an alias of the (int) cast. (boolean) is an alias of the (bool) cast. (binary) is an alias of the (string) cast. (double) and (real) are aliases of the (float) cast. These casts do not use the canonical type name and are not recommended.

Warnung

The (real) cast alias has been deprecated as of PHP 8.0.0.

Warnung

The (unset) cast has been deprecated as of PHP 7.2.0. Note that the (unset) cast is the same as assigning the value NULL to the variable or call. The (unset) cast is removed as of PHP 8.0.0.

Achtung

The (binary) cast and b prefix exists for forward support. Currently (binary) and (string) are identical, however this may change and should not be relied upon.

Hinweis:

Whitespaces are ignored within the parentheses of a cast. Therefore, the following are two casts are equivalent:

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

Casting literal strings and variables to binary strings:

<?php
$binary 
= (binary) $string;
$binary b"binary string";
?>

Hinweis: Instead of casting a variable to a string, it is also possible to enclose the variable in double quotes.

<?php
$foo 
10;            // $foo is an integer
$str "$foo";        // $str is a string
$fst = (string) $foo// $fst is also a string

// This prints out that "they are the same"
if ($fst === $str) {
    echo 
"they are the same";
}
?>

It may not be obvious exactly what will happen when casting between certain types. For more information, see these sections:

Hinweis: Because PHP supports indexing into strings via offsets using the same syntax as array indexing, the following example holds true for all PHP versions:

<?php
$a    
'car'// $a is a string
$a[0] = 'b';   // $a is still a string
echo $a;       // bar
?>
See the section titled String access by character for more information.

Hier Kannst Du einen Kommentar verfassen


Bitte gib mindestens 10 Zeichen ein.
Wird geladen... Bitte warte.
* Pflichtangabe
Es sind noch keine Kommentare vorhanden.

Midjourney Tutorial - Anleitung für Anfänger

Über Midjourney, dem Tool zur Erstellung digitaler Bilder mithilfe von künstlicher Intelligenz, gibt es ein informatives Video mit dem Titel "Midjourney Tutorial auf Deutsch - Anleitung für Anfänger" ...

Mike94

Autor : Mike94
Kategorie: KI Tutorials

Grundlagen von Views in MySQL

Views in einer MySQL-Datenbank bieten die Möglichkeit, eine virtuelle Tabelle basierend auf dem Ergebnis einer SQL-Abfrage zu erstellen. ...

admin

Autor : admin
Kategorie: mySQL-Tutorials

Definition von Stored Procedures - eine Einführung

Stored Procedures sind vordefinierte SQL-Codeblöcke, die in einer Datenbank gespeichert sind und bei Bedarf aufgerufen werden können. ...

Bernie

Autor : ebiz-consult GmbH & Co. KG
Kategorie: mySQL-Tutorials

Tutorial veröffentlichen

Tutorial veröffentlichen

Teile Dein Wissen mit anderen Entwicklern weltweit

Du bist Profi in deinem Bereich und möchtest dein Wissen teilen, dann melde dich jetzt an und teile es mit unserer PHP-Community

mehr erfahren

Tutorial veröffentlichen

emails bei neuen Jobs

Hallo zusammen, ich möchte das Team von akadhilfe24 für ghostwriting bachelorarbeit (https://akadhilfe24.de/bachelorarbeit-schreiben-lassen/) em ...

Geschrieben von VittoCheri am 25.04.2024 22:51:10
Forum: Fragen/Vorschläge zum Forum
Berechnungen durchführen

Hallo liebe Forenmitglieder, meine erste frage ist zum Aufbau meiner kleinen Berechnungswebseite, nichts kommerzielles, soll nur eine Anwendung f ...

Geschrieben von matze511 am 21.04.2024 21:42:37
Forum: PHP Developer Forum
Professioneller Webentwickler & Webdesigner

Of course, here is the translation: Hello, Thank you for your interest in the long-term project. Your extensive skills and experience in web dev ...

Geschrieben von Athelstan am 15.04.2024 09:25:39
Forum: Jobgesuche
Wir stellen unsere SEO-Agentur vor

Hallo In der heutigen digitalen Welt war es für Unternehmen noch nie so einfach, ihre Reichweite weltweit zu vergrößern. Wenn Sie außerhalb I ...

Geschrieben von thomasmuller am 14.04.2024 07:18:33
Forum: User stellen sich vor