<html xmlns="http://www.w3.org/1999/xhtml" lang="en">
<head><meta content="application/xhtml+xml; charset=utf-8" http-equiv="Content-Type"/>
<meta content="application/xhtml+xml; charset=utf-8" name="content-type"/>
<meta content="Using the PHP Language" name="title"/>
<meta content="Philip Ackermann" name="author"/>
<meta content="Rheinwerk Publishing" name="publisher"/>
<meta content="© 2023 by Rheinwerk Publishing Inc., Boston (MA)" name="copyright"/>
<meta content="Full Stack Web Development - The Comprehensive Guide - Using the PHP Language" name="description"/>
<meta content="en" name="language"/>
<title>Using the PHP Language</title>
<link href="common/main.css" rel="stylesheet" type="text/css"/>
<meta content="urn:uuid:e0000000-0000-0000-0000-000030723404" name="Adept.expected.resource"/>
</head>
<body class="office_us type_kapitel">
<div id="main">
<h2 class="t2" id="h15.3">15.3 Variables, Data Types, and Operators</h2>
<p class="standard">Once MAMP has been set up, you’re ready to go. However, before we turn to a concrete example, I first want to discuss the basic programming structure<a class="indexanchor" id="i15_14"/>. For this purpose, you’ll need to create a file called <span class="italic">test.php</span> and open it in an editor of your choice. This file will be placed in the folder you specified earlier. As shown in <span class="crossreference "><a href="15_002.html#f15.1">Figure 15.1</a></span>, for me, this folder is the <span class="italic">Sites/localhost</span> directory, which is located in my user folder. Within the test file, you can specify any HTML code you like.</p>
<p class="standard">You can then define PHP areas<a class="indexanchor" id="i15_15"/> within the opening <samp class="listingcharacter programmingelement"><?php</samp> and closing <samp class="listingcharacter programmingelement">?></samp> tags, as shown in <span class="crossreference "><a href="15_003.html#l15.1">Listing 15.1</a></span>. <samp class="listingcharacter programmingelement">echo</samp> outputs “Hello World.” <span class="crossreference "><a href="15_003.html#f15.3">Figure 15.3</a></span> shows how the <span class="italic">http://localhost:8888/test.php</span> address was called in the browser. As expected, PHP returns the corresponding string.</p>
<div class="box box_standard">
<h3 class="boxheading">Opening and Closing PHP Tags</h3>
<p class="standard first last">Note that the closing PHP tags at the end of a file can be omitted. As a matter of fact, omission is even recommended (<span class="url"><a href="https://www.php.net/manual/de/language.basic-syntax.phptags.php">https://www.php.net/manual/de/language.basic-syntax.phptags.php</a></span>). Skipping the closing tag ensures that no spaces or blank lines are accidentally sent to the browser afterwards. Otherwise, an unwanted output would be created that was not intended by the programmer. Even worse, functions that depend on HTTP headers often fail, for example, sessions, cookies, or redirects.</p>
</div>
<div class="listing " id="l15.1"><pre><span class="dunkelblau"><a id="p428"/><html></span><span class="schwarz"/><br/><span class="dunkelblau"><head></span><span class="schwarz"/><br/> <span class="dunkelblau"><title></span><span class="schwarz">PHP Test</span><span class="dunkelblau"></title></span><span class="schwarz"/><br/><span class="dunkelblau"></head></span><span class="schwarz"/><br/><span class="dunkelblau"><body></span><span class="schwarz"/><br/> <span class="dunkelblau"><?php echo '<p></span><span class="gelbgruen">Hello World</span><span class="dunkelblau"></p></span><span class="gelbgruen">'; ?></span><span class="schwarz"><br/></span><span class="dunkelblau"></body></span><span class="schwarz"/><br/><span class="dunkelblau"></html></span><span class="schwarz"> </span><span class="schwarz"/></pre></div>
<p class="caption "><b>Listing 15.1</b>
Opening and Closing PHP Tags</p>
<div class="imagebox figure-type"><a href="img-f15.3.html" id="f15.3"><img alt="This test.php File Returns “Hello World” When Called in the Browser" id="img-f15.3" src="bilderklein/klein15_003.png"/></a></div>
<p class="caption "><b>Figure 15.3</b>
This test.php File Returns “Hello World” When Called in the Browser</p>
<h3 class="t3" id="h15.3.1">15.3.1 Using Variables</h3>
<p class="standard"><a class="indexanchor" id="i15_16"/><span class="crossreference "><a href="15_003.html#l15.2">Listing 15.2</a></span> shows how a variable is usually declared<a class="indexanchor" id="i15_17"/> and initialized<a class="indexanchor" id="i15_18"/>, always starting with a dollar sign<a class="indexanchor" id="i15_19"/> (<samp class="listingcharacter programmingelement">$</samp>), followed by a name (which may consist of uppercase and lowercase letters), and an underscore (<samp class="listingcharacter programmingelement">_</samp>). Numbers may also appear, but not at the beginning, or more precisely not directly after the dollar sign. In addition, PHP has some naming guidelines that you should follow (<span class="url"><a href="https://www.php.net/manual/en/userlandnaming.php">https://www.php.net/manual/en/userlandnaming.php</a></span>). A standalone declaration like in JavaScript (see <span class="crossreference "><a href="04_001.html#h4">Chapter 4</a></span>, <span class="crossreference "><a href="04_002.html#h4.2.1">Section 4.2.1</a></span>) does not exist in PHP. Likewise, the <span class="italic">casting</span>, represented in our example by the preceding <samp class="listingcharacter programmingelement">(string)</samp>, can usually be omitted since PHP automatically recognizes or defines the type of the variable.<a class="indexanchor" id="i15_20"/> (More on this topic shortly.)</p>
<div class="listing " id="l15.2"><pre><span class="gruen"><?php</span><span class="schwarz"><br/></span><span class="hellblau">$firstName</span><span class="schwarz"> = </span><span class="braun">'John'</span><span class="schwarz">; </span><span class="gelbgruen"> // Variable declaration and initialization</span><span class="schwarz"><br/></span><span class="hellblau">$firstName</span><span class="schwarz"> = (string) </span><span class="braun">'John'</span><span class="schwarz">; </span><span class="gelbgruen"> // Type casting</span><span class="schwarz"> </span><span class="schwarz"/></pre></div>
<p class="caption "><b>Listing 15.2</b>
Declaring Variables</p>
<p class="standard"><a id="p429"/>PHP offers several primitive data types,<a class="indexanchor" id="i15_21"/> listed in <span class="crossreference "><a href="15_003.html#t15.2">Table 15.2</a></span>.</p>
<table class="standardtable" id="t15.2">
<thead>
<tr>
<th class="tablehead tablecell_first top_border_cell">
<p class="standard first-item last-item">Data Type</p>
</th>
<th class="tablehead tablecell_last top_border_cell">
<p class="standard first-item last-item">Description</p>
</th>
</tr>
</thead>
<tbody>
<tr class="tablehead2">
<td class="tablehead2 tablecell_first tablecell_last top_border_cell tablecell_has_colspan" colspan="2">
<p class="standard first-item last-item">Scalar Types<a class="indexanchor" id="i15_22"/></p>
</td>
</tr>
<tr class="light">
<td class="tablecell tablecell_first">
<p class="standard first-item last-item"><samp class="listingcharacter programmingelement">bool</samp></p>
</td>
<td class="tablecell tablecell_last">
<p class="standard first-item last-item">Boolean value <samp class="listingcharacter programmingelement">true</samp> or <samp class="listingcharacter programmingelement">false</samp></p>
</td>
</tr>
<tr>
<td class="tablecell tablecell_first">
<p class="standard first-item last-item"><samp class="listingcharacter programmingelement">int</samp></p>
</td>
<td class="tablecell tablecell_last">
<p class="standard first-item last-item">Integer value (integer)</p>
</td>
</tr>
<tr class="light">
<td class="tablecell tablecell_first">
<p class="standard first-item last-item"><samp class="listingcharacter programmingelement">float</samp></p>
</td>
<td class="tablecell tablecell_last">
<p class="standard first-item last-item">Floating point number, also referred to as <samp class="listingcharacter programmingelement">double</samp></p>
</td>
</tr>
<tr>
<td class="tablecell tablecell_first">
<p class="standard first-item last-item"><samp class="listingcharacter programmingelement">string</samp></p>
</td>
<td class="tablecell tablecell_last">
<p class="standard first-item last-item">Character string</p>
</td>
</tr>
<tr class="tablehead2 light">
<td class="tablehead2 tablecell_first tablecell_last tablecell_has_colspan" colspan="2">
<p class="standard first-item last-item">Compound Types<a class="indexanchor" id="i15_23"/></p>
</td>
</tr>
<tr>
<td class="tablecell tablecell_first">
<p class="standard first-item last-item"><samp class="listingcharacter programmingelement">array</samp></p>
</td>
<td class="tablecell tablecell_last">
<p class="standard first-item last-item">A map<a class="indexanchor" id="i15_24"/> that assigns values to keys</p>
</td>
</tr>
<tr class="light">
<td class="tablecell tablecell_first">
<p class="standard first-item last-item"><samp class="listingcharacter programmingelement">object</samp></p>
</td>
<td class="tablecell tablecell_last">
<p class="standard first-item last-item">Instance of a class</p>
</td>
</tr>
<tr>
<td class="tablecell tablecell_first">
<p class="standard first-item last-item"><samp class="listingcharacter programmingelement">callable</samp></p>
</td>
<td class="tablecell tablecell_last">
<p class="standard first-item last-item">Callback function</p>
</td>
</tr>
<tr class="light">
<td class="tablecell tablecell_first">
<p class="standard first-item last-item"><samp class="listingcharacter programmingelement">iterable</samp></p>
</td>
<td class="tablecell tablecell_last">
<p class="standard first-item last-item">A pseudo-type that accepts an array or an object<a class="indexanchor" id="i15_25"/> that implements the <samp class="listingcharacter programmingelement">traversable</samp> interface</p>
</td>
</tr>
<tr class="tablehead2">
<td class="tablehead2 tablecell_first tablecell_last tablecell_has_colspan" colspan="2">
<p class="standard first-item last-item">Special Types<a class="indexanchor" id="i15_26"/></p>
</td>
</tr>
<tr class="light">
<td class="tablecell tablecell_first">
<p class="standard first-item last-item"><samp class="listingcharacter programmingelement">resource</samp></p>
</td>
<td class="tablecell tablecell_last">
<p class="standard first-item last-item">A resource<a class="indexanchor" id="i15_27"/> is a special variable that is a reference to an external resource. For example, when a file is opened for writing, the connection remains (as a resource) until it is closed.</p>
</td>
</tr>
<tr>
<td class="tablecell tablecell_first">
<p class="standard first-item last-item"><samp class="listingcharacter programmingelement">NULL</samp></p>
</td>
<td class="tablecell tablecell_last">
<p class="standard first-item last-item">Can be used to describe a variable without a value.</p>
</td>
</tr>
</tbody>
</table>
<p class="caption "><b>Table 15.2</b>
The Ten Primitive Data Types of PHP</p>
<h4 class="t4" id="h15.3.1.1">Juggling with Types (Type Juggling)</h4>
<p class="standard"><a class="indexanchor" id="i15_28"/><a class="indexanchor" id="i15_29"/>As mentioned earlier, PHP does not require or support an explicit type definition in the variable declaration<a class="indexanchor" id="i15_30"/>. The type of a variable is instead determined by the concrete value that the variable contains. In other words, if a string value is assigned to the <samp class="listingcharacter programmingelement">$var</samp> variable, <samp class="listingcharacter programmingelement">$var</samp> becomes a string. If <samp class="listingcharacter programmingelement">$var</samp> is subsequently assigned an <samp class="listingcharacter programmingelement">int</samp> value, it becomes an integer. <span class="crossreference "><a href="15_003.html#l15.3">Listing 15.3</a></span> shows how PHP proceeds.</p>
<div class="listing " id="l15.3"><pre><span class="gruen"><?php</span><span class="schwarz"><br/></span><span class="hellblau">$var</span><span class="schwarz"> = </span><span class="braun">'1'</span><span class="schwarz">; </span><span class="gelbgruen"> // $var is a string.</span><span class="schwarz"><br/></span><span class="hellblau">$var</span><span class="schwarz"> = </span><span class="hellblau">$var</span><span class="schwarz"> * </span><span class="violett">1</span><span class="schwarz">; </span><span class="gelbgruen"> // $var is now an integer.</span><span class="schwarz"><br/></span><span class="hellblau">$var</span><span class="schwarz"> = (string) </span><span class="hellblau">$var</span><span class="schwarz">; </span><span class="gelbgruen"> // $var is a string again.</span><span class="schwarz"> </span><span class="schwarz"/></pre></div>
<p class="caption "><b>Listing 15.3</b>
Type Juggling in PHP</p>
<p class="standard">To force a variable to evaluate as a specific type, you can cast it or use the <samp class="listingcharacter programmingelement">settype()</samp> function (<span class="url"><a href="https://www.php.net/manual/en/function.settype.php">https://www.php.net/manual/en/function.settype.php</a></span>). Casting (in the last <a id="p430"/>line of our example) converts the value to a specific type by writing the type in parentheses before the value to be converted. The following values are allowed:</p>
<ul>
<li>
<p class="standard first-item last-item"><samp class="listingcharacter programmingelement">(int)</samp> or <samp class="listingcharacter programmingelement">(integer)</samp></p>
</li>
<li>
<p class="standard first-item last-item"><samp class="listingcharacter programmingelement">(bool)</samp> or <samp class="listingcharacter programmingelement">(boolean)</samp></p>
</li>
<li>
<p class="standard first-item last-item"><samp class="listingcharacter programmingelement">(float)</samp> or <samp class="listingcharacter programmingelement">(double)</samp> (in PHP 7 and earlier, <samp class="listingcharacter programmingelement">(real)</samp> is also possible)</p>
</li>
<li>
<p class="standard first-item last-item"><samp class="listingcharacter programmingelement">(string)</samp> or <samp class="listingcharacter programmingelement">(binary)</samp></p>
</li>
<li>
<p class="standard first-item last-item"><samp class="listingcharacter programmingelement">(array)</samp></p>
</li>
<li>
<p class="standard first-item last-item"><samp class="listingcharacter programmingelement">(object)</samp></p>
</li>
<li>
<p class="standard first-item last-item"><samp class="listingcharacter programmingelement">(unset)</samp> (value allowed only in PHP 7 and earlier)</p>
</li>
</ul>
<p class="standard">We recommend using only the values named first, for example, <samp class="listingcharacter programmingelement">(int)</samp> instead of <samp class="listingcharacter programmingelement">(integer)</samp> because the second values are only aliases. These aliases could possibly disappear in later PHP versions, just as <samp class="listingcharacter programmingelement">(real)</samp> is no longer possible in PHP 8.</p>
<h4 class="t4" id="h15.3.1.2">References</h4>
<p class="standard">A variable does not always have to be assigned a value. Instead, you can also pass it a reference<a class="indexanchor" id="i15_31"/> by using the <samp class="listingcharacter programmingelement">&</samp> operator <a class="indexanchor" id="i15_32"/>, as shown in <span class="crossreference "><a href="15_003.html#l15.4">Listing 15.4</a></span>. A reference is therefore a pointer to another variable. If a reference is changed, the value of the referenced variable changes as well.</p>
<div class="listing " id="l15.4"><pre><span class="gruen"><?php</span><span class="schwarz"><br/></span><span class="hellblau">$lastName</span><span class="schwarz"> = </span><span class="braun">'Doe'</span><span class="schwarz">;</span><br/><span class="hellblau">$birthName</span><span class="schwarz"> = </span><span class="bold">&</span><span class="hellblau">$lastName</span><span class="schwarz">; </span><span class="gelbgruen"> // now also contains the value 'Doe'.</span><span class="schwarz"><br/></span><span class="hellblau">$birthName</span><span class="schwarz"> = </span><span class="braun">'samplewife'</span><span class="schwarz">; </span><span class="gelbgruen"> // $birthName is changed. Thereby</span><span class="schwarz"><br/></span> <span class="gelbgruen"> // also $lastName.</span><span class="schwarz"><br/></span><span class="rot">echo</span><span class="schwarz"> </span><span class="hellblau">$lastName</span><span class="schwarz">; </span><span class="gelbgruen"> // Output of $lastName: 'Deer'.</span><span class="schwarz"> </span><span class="schwarz"/></pre></div>
<p class="caption "><b>Listing 15.4</b>
Assigning a Reference</p>
<h4 class="t4" id="h15.3.1.3">Predefined Variables</h4>
<p class="standard"><a class="indexanchor" id="i15_33"/>In PHP, some variables are made available depending on the current namespace (see box). For a complete list, you should visit <span class="url"><a href="https://www.php.net/manual/en/reserved.variables.php">https://www.php.net/manual/en/reserved. variables.php</a></span>. As an example, the <samp class="listingcharacter programmingelement">$_GET</samp> variable contains all parameters given to the script via the Uniform Resource Locator (URL) <span class="italic">http://localhost:8888/test.php?firstName=John&lastName=Doe.</span> <span class="crossreference "><a href="15_003.html#l15.5">Listing 15.5</a></span> shows the output of the variables from the script shown in <span class="crossreference "><a href="15_003.html#l15.6">Listing 15.6</a></span>.</p>
<div class="listing " id="l15.5"><pre>array (size=2)<br/> 'firstName' => string 'John' (length=4)<br/> 'lastName' => string 'Doe' (length=3) </pre></div>
<p class="caption "><b>Listing 15.5</b>
$_GET Variable Now Containing the Two Values Passed by the URL</p>
<div class="listing " id="l15.6"><pre><span class="gruen"><a id="p431"/><?php</span><span class="schwarz"><br/></span><span class="violett">var_dump</span><span class="schwarz">( </span><span class="hellblau">$_GET</span><span class="schwarz"> ); </span><span class="schwarz"/></pre></div>
<p class="caption "><b>Listing 15.6</b>
var_dump() Providing a Nice Output in the Browser</p>
<div class="box box_standard">
<h5 class="boxheading">Namespace</h5>
<p class="standard first">When you define variables, functions, or classes, they are placed in the global namespace<a class="indexanchor" id="i15_34"/>. Thus, for example, you can access a constant even if the constant is defined in a completely different file. Of course, this file must be included using one of the keywords <samp class="listingcharacter programmingelement">include</samp>, <samp class="listingcharacter programmingelement">require</samp>, <samp class="listingcharacter programmingelement">include_once</samp>, or <samp class="listingcharacter programmingelement">require_once</samp> (see <span class="url"><a href="https://www.php.net/manual/en/language.control-structures.php">https://www.php.net/manual/en/language.control-structures.php</a></span>), as shown in the following examples.</p>
<div class="listing " id="l15.7"><pre><span class="gruen"><?php</span><span class="schwarz"><br/></span><span class="rot">const</span><span class="schwarz"> FOO = </span><span class="braun">"Hello World"</span><span class="schwarz">;</span><span class="schwarz"/></pre></div>
<p class="caption "><b>Listing 15.7</b>
hello-world.php file</p>
<div class="listing " id="l15.8"><pre><span class="gruen"><?php</span><span class="schwarz"><br/></span><span class="rot">include</span><span class="schwarz"> </span><span class="braun">"hello-world.php"</span><span class="schwarz">;</span><br/><span class="rot">echo</span><span class="schwarz"> FOO;</span><span class="schwarz"/></pre></div>
<p class="caption "><b>Listing 15.8</b>
test.php File</p>
<p class="standard">When <span class="italic">test.php</span> is called, the browser will output “Hello World.”</p>
<p class="standard">However, you can also define your own namespaces by using the <samp class="listingcharacter programmingelement">namespace</samp> keyword, as shown in <span class="crossreference "><a href="15_003.html#l15.9">Listing 15.9</a></span>. The <samp class="listingcharacter programmingelement">FOO</samp> constant is now no longer in the global namespace but instead in the <samp class="listingcharacter programmingelement">hello_world</samp> namespace.</p>
<div class="listing " id="l15.9"><pre><span class="gruen"><?php</span><span class="schwarz"><br/></span><span class="rot">namespace</span><span class="schwarz"> hello_world;</span><br/><span class="rot">const</span><span class="schwarz"> FOO = </span><span class="braun">"Hello World"</span><span class="schwarz">;</span><span class="schwarz"/></pre></div>
<p class="caption "><b>Listing 15.9</b>
hello-world.php File with Namespace</p>
<p class="standard">The constant can now be accessed only via an additional specification of the namespace.</p>
<div class="listing " id="l15.10"><pre><span class="gruen"><?php</span><span class="schwarz"><br/></span><span class="rot">include</span><span class="schwarz"> </span><span class="braun">"hello-world.php"</span><span class="schwarz">;</span><br/><span class="rot">echo</span><span class="schwarz"> </span><span class="bold">\hello_world\</span>FOO;<span class="schwarz"/></pre></div>
<p class="caption "><b>Listing 15.10</b>
test.php File with Access to the FOO Constant of a Different Namespace</p>
<p class="standard last">Note that the namespace must be enclosed in backslashes (<samp class="listingcharacter programmingelement">\</samp>) when referenced.</p>
</div>
<div class="box box_standard">
<h5 class="boxheading"><a id="p432"/>Scope and Variable Variables<a class="indexanchor" id="i15_35"/></h5>
<p class="standard first">Variables are only valid in certain areas, as is the case in nearly every programming language. You’ll learn more about functions in <span class="crossreference "><a href="15_005.html#h15.5">Section 15.5</a></span>, but note that variables defined in functions do not exist outside their functions. Some exceptions exist, however, and the <samp class="listingcharacter programmingelement">global</samp><a class="indexanchor" id="i15_36"/> keyword enables you to bypass this restriction. In most cases, however, global variables should be avoided, so I won’t describe them in greater detail. To learn more about this topic, as well as for an explanation of the <samp class="listingcharacter programmingelement">static</samp><a class="indexanchor" id="i15_37"/> keyword, check out <span class="url"><a href="https://www.php.net/manual/en/language.variables.scope.php">https://www.php.net/manual/en/language.variables.scope.php</a></span>.</p>
<p class="standard last"><span class="italic">Variable variables</span> are somewhat more complex<a class="indexanchor" id="i15_38"/>. To use this type of dynamic variable, detailed instructions are available at <span class="url"><a href="https://www.php.net/manual/en/language.variables.variable.php">https://www.php.net/manual/en/language.variables.variable.php</a></span>.</p>
</div>
<h3 class="t3" id="h15.3.2">15.3.2 Using Constants</h3>
<p class="standard">Constants<a class="indexanchor" id="i15_39"/> are variables that cannot be changed. Constants are defined by the <samp class="listingcharacter programmingelement">define()</samp> function, as shown in <span class="crossreference "><a href="15_003.html#l15.11">Listing 15.11</a></span>, where you can also see how the call is made, namely, <span class="italic">without</span> a preceding dollar sign.</p>
<p class="standard">Constants can also be written in lowercase, but the general convention is to use uppercase letters. Constants are always global, which means they can be accessed from anywhere, from a function as well as from a class method.</p>
<div class="listing " id="l15.11"><pre><span class="gruen"><?php</span><span class="schwarz"><br/></span><span class="violett">define</span><span class="schwarz">( </span><span class="braun">'MINUTE_IN_SECONDS'</span><span class="schwarz">, </span><span class="violett">60</span><span class="schwarz"> );</span><br/><span class="violett">define</span><span class="schwarz">( </span><span class="braun">'HOUR_IN_SECONDS'</span><span class="schwarz">, </span><span class="violett">60</span><span class="schwarz"> * MINUTE_IN_SECONDS );</span><br/><span class="violett">define</span><span class="schwarz">( </span><span class="braun">'DAY_IN_SECONDS'</span><span class="schwarz">, </span><span class="violett">24</span><span class="schwarz"> * HOUR_IN_SECONDS ); </span><span class="schwarz"/></pre></div>
<p class="caption "><b>Listing 15.11</b>
Defining Constants</p>
<div class="box box_standard">
<h4 class="boxheading">Predefined and Magic Constants</h4>
<p class="standard first last">PHP uses some predefined<a class="indexanchor" id="i15_40"/> constants/magic constants<a class="indexanchor" id="i15_41"/>. Predefined<a class="indexanchor" id="i15_42"/> constants start with a double underscore: <samp class="listingcharacter programmingelement">__FILE__</samp>, for example, returns the full path and file name of the file that’s currently being executed. You should avoid declaring constants in your projects that start with a double underscore<a class="indexanchor" id="i15_43"/> or with <samp class="listingcharacter programmingelement">PHP_</samp>, as PHP itself may one day introduce a constant with the same name. For a complete current list, refer to <span class="url"><a href="https://www.php.net/manual/en/language.constants.magic.php">https://www.php.net/manual/en/language.constants.magic.php</a></span> and <span class="url"><a href="https://www.php.net/manual/en/reserved.constants.php">https://www.php.net/manual/en/reserved.constants.php</a></span>.</p>
</div>
<h3 class="t3" id="h15.3.3">15.3.3 Using Operators</h3>
<p class="standard"><a class="indexanchor" id="i15_44"/>As in all other programming languages, many operators are available in PHP that allow you to compare or modify variables, such as the following:</p>
<ul>
<li>
<p class="standard first-item last-item"><span class="italic"><a id="p433"/>Arithmetic operators</span><a class="indexanchor" id="i15_45"/> are used for calculations. The following are available: <samp class="listingcharacter programmingelement">+</samp> (addition<a class="indexanchor" id="i15_46"/>), <samp class="listingcharacter programmingelement">-</samp> (subtraction<a class="indexanchor" id="i15_47"/>), <samp class="listingcharacter programmingelement">*</samp> (multiplication<a class="indexanchor" id="i15_48"/>), <samp class="listingcharacter programmingelement">/</samp> (division<a class="indexanchor" id="i15_49"/>), and <samp class="listingcharacter programmingelement">%</samp> (modulo<a class="indexanchor" id="i15_50"/>). In addition, these operators include the increment<a class="indexanchor" id="i15_51"/> and decrement operators<a class="indexanchor" id="i15_52"/> (<samp class="listingcharacter programmingelement">++</samp> and <samp class="listingcharacter programmingelement">--</samp>), which add or subtract 1 from a number. A power of a number can be calculated by using <samp class="listingcharacter programmingelement">**</samp>.</p>
</li>
<li>
<p class="standard first-item last-item">The <span class="italic">assignment operator</span><a class="indexanchor" id="i15_53"/> is the equal sign (<samp class="listingcharacter programmingelement">=</samp>). You’ve already encountered this operator whenever we’ve set values for variables. In addition, <span class="italic">combined operators</span> allow you to use the value of a variable in an expression and then assign the result of that expression as a new value (shown later in <span class="crossreference "><a href="15_003.html#l15.17">Listing 15.17</a></span>). These operators include <samp class="listingcharacter programmingelement">+=</samp> (for addition), <samp class="listingcharacter programmingelement">-=</samp> (for subtraction, <samp class="listingcharacter programmingelement">*=</samp> (for multiplication), <samp class="listingcharacter programmingelement">/=</samp> (for division), <samp class="listingcharacter programmingelement">%=</samp> (for modulus calculation), <samp class="listingcharacter programmingelement">**=</samp> (for exponentiation), and <samp class="listingcharacter programmingelement">.=</samp> (the string operator for concatenation<a class="indexanchor" id="i15_54"/>).</p>
</li>
<li>
<p class="standard first-item last-item"><span class="italic">Bit operators</span><a class="indexanchor" id="i15_55"/> allow you to check and manipulate specific bits in an integer. The following bit operators are available: <samp class="listingcharacter programmingelement">&</samp> (bitwise AND<a class="indexanchor" id="i15_56"/>), <samp class="listingcharacter programmingelement">|</samp> (bitwise OR<a class="indexanchor" id="i15_57"/>), <samp class="listingcharacter programmingelement">^</samp> (bitwise XOR<a class="indexanchor" id="i15_58"/>), <samp class="listingcharacter programmingelement">~</samp> (bitwise NOT<a class="indexanchor" id="i15_59"/>), <samp class="listingcharacter programmingelement"><<</samp> (bitwise shift<a class="indexanchor" id="i15_60"/> to the left), and <samp class="listingcharacter programmingelement">>></samp> (bitwise shift to the right).</p>
</li>
<li>
<p class="standard first-item"><span class="italic">Comparison operators</span><a class="indexanchor" id="i15_61"/> allow you to compare values directly. The following operators are available: <samp class="listingcharacter programmingelement">==</samp> (equal to<a class="indexanchor" id="i15_62"/>), <samp class="listingcharacter programmingelement">===</samp> (identical to<a class="indexanchor" id="i15_63"/>), <samp class="listingcharacter programmingelement">!=</samp> or <samp class="listingcharacter programmingelement"><></samp> (not equal to<a class="indexanchor" id="i15_64"/>), <samp class="listingcharacter programmingelement">!==</samp> = (not identical to), <samp class="listingcharacter programmingelement"><</samp> (less than), <samp class="listingcharacter programmingelement">></samp> (greater than), <samp class="listingcharacter programmingelement"><=</samp> (less than or equal to), <samp class="listingcharacter programmingelement">>=</samp> (greater than or equal to), and <samp class="listingcharacter programmingelement"><=></samp> (spaceship, see box). “Identical to” in PHP means not only is the value identical but also its type. Refer to <span class="crossreference "><a href="15_003.html#l15.12">Listing 15.12</a></span> for more information on this topic.</p>
<div class="listing " id="l15.12"><pre><span class="gruen"><?php</span><span class="schwarz"><br/></span><span class="hellblau">$a</span><span class="schwarz"> = </span><span class="braun">'1'</span><span class="schwarz">;</span><br/><span class="hellblau">$b</span><span class="schwarz"> = </span><span class="violett">1</span><span class="schwarz">;</span><br/><br/><span class="hellblau">$c</span><span class="schwarz"> = </span><span class="hellblau">$a</span><span class="schwarz"> == </span><span class="hellblau">$b</span><span class="schwarz">; </span><span class="gelbgruen"> // Returns true.</span><span class="schwarz"><br/></span><span class="hellblau">$d</span><span class="schwarz"> = </span><span class="hellblau">$a</span><span class="schwarz"> === </span><span class="hellblau">$b</span><span class="schwarz">;</span><span class="gelbgruen"> // Returns false because the type comparison fails.</span><span class="schwarz"> </span><span class="schwarz"/></pre></div>
<p class="caption "><b>Listing 15.12</b>
Comparisons of Equality and Identity</p>
</li>
<li>
<p class="standard first-item last-item"><a class="indexanchor" id="i15_65"/><span class="italic">Operator for program execution: Backticks</span> (<samp class="listingcharacter programmingelement">``</samp>) can be used to execute external programs. Note that backticks are not simple execution characters and that execution will fail if the <samp class="listingcharacter programmingelement">shell_exec()</samp> function has been disabled. The line <samp class="listingcharacter programmingelement">$file_list = `ls -al`;</samp> tries to execute the <samp class="listingcharacter programmingelement">ls</samp> command line tool, which lists all files and directories of the current folder.</p>
</li>
<li>
<p class="standard first-item last-item"><span class="italic">Logical operators</span><a class="indexanchor" id="i15_66"/> are for using Boolean values. The following operators can be used: <samp class="listingcharacter programmingelement">and</samp> or <samp class="listingcharacter programmingelement">&&</samp> (logical AND<a class="indexanchor" id="i15_67"/>), <samp class="listingcharacter programmingelement">or</samp> or <samp class="listingcharacter programmingelement">||</samp> (logical OR<a class="indexanchor" id="i15_68"/>), <samp class="listingcharacter programmingelement">xor</samp> (exclusive OR<a class="indexanchor" id="i15_69"/>), and <samp class="listingcharacter programmingelement">!</samp> (negation).</p>
</li>
<li>
<p class="standard first-item last-item"><span class="italic">Array operators</span><a class="indexanchor" id="i15_70"/> are used to compare or manipulate arrays. <samp class="listingcharacter programmingelement">$a + $b</samp> unites two arrays. <samp class="listingcharacter programmingelement">==</samp> returns <samp class="listingcharacter programmingelement">true</samp> if both arrays contain the same key-value pairs. <samp class="listingcharacter programmingelement">===</samp> returns <samp class="listingcharacter programmingelement">true</samp> if both arrays contain the same key-value pairs in the same order and they are of the same type. Accordingly, <samp class="listingcharacter programmingelement">!=</samp> or <samp class="listingcharacter programmingelement"><></samp> mean that two arrays are not equal. <samp class="listingcharacter programmingelement">!==</samp> in turn means “not identical,” where a type comparison is also performed.</p>
</li>
<li>
<p class="standard first-item last-item"><a class="indexanchor" id="i15_71"/><span class="italic">Type operator:</span> The <samp class="listingcharacter programmingelement">instanceof</samp> keyword checks if an object belongs to a certain class.</p>
</li>
<li>
<p class="standard first-item"><a id="p434"/>The <span class="italic">ternary operator</span><a class="indexanchor" id="i15_72"/> (shown in <span class="crossreference "><a href="15_003.html#l15.13">Listing 15.13</a></span>) is another comparison operator that returns an expression and replaces a longer <samp class="listingcharacter programmingelement">if</samp> statement. This operator also available in the short version, where the middle part can be omitted, as shown in <span class="crossreference "><a href="15_003.html#l15.14">Listing 15.14</a></span>.</p>
<div class="listing " id="l15.13"><pre><span class="gruen"><?php</span><span class="schwarz"><br/></span><span class="hellblau">$action</span><span class="schwarz"> = ( </span><span class="rot">empty</span><span class="schwarz">( </span><span class="hellblau">$_POST</span><span class="schwarz">[</span><span class="braun">'action'</span><span class="schwarz">] ) ) ? </span><span class="braun">'standard'</span><span class="schwarz"> : </span><span class="hellblau">$_POST</span><span class="schwarz">[</span><span class="braun">'action'</span><span class="schwarz">];</span><br/><span class="gelbgruen"><br/></span>// The above is identical to the IF statement below.<span class="schwarz"><br/></span><span class="rot">if</span><span class="violett"> </span><span class="schwarz">(</span><span class="rot">empty</span><span class="schwarz">(</span><span class="hellblau">$_POST</span><span class="schwarz">[</span><span class="braun">'action'</span><span class="schwarz">])) {</span><br/> <span class="hellblau">$action</span><span class="schwarz"> = </span><span class="braun">'standard'</span><span class="schwarz">;</span><br/>} <span class="rot">else</span><span class="schwarz"> {</span><br/> <span class="hellblau">$action</span><span class="schwarz"> = </span><span class="hellblau">$_POST</span><span class="schwarz">[</span><span class="braun">'action'</span><span class="schwarz">];</span><br/>} <span class="schwarz"/></pre></div>
<p class="caption "><b>Listing 15.13</b>
Ternary Operator</p>
<div class="listing " id="l15.14"><pre><span class="gruen"><?php</span><span class="schwarz"><br/></span><span class="hellblau">$firstName</span><span class="schwarz"> = </span><span class="braun">''</span><span class="schwarz">;</span><br/><span class="hellblau">$firstName</span><span class="schwarz"> = </span><span class="hellblau">$firstName</span><span class="schwarz"> ?: </span><span class="braun">'John'</span><span class="schwarz">;</span><span class="gelbgruen"> // Contains 'John'.</span><span class="schwarz"><br/></span><br/><span class="hellblau">$firstName</span><span class="schwarz"> = </span><span class="braun">'Anne'</span><span class="schwarz">;</span><br/><span class="hellblau">$firstName</span><span class="schwarz"> = </span><span class="hellblau">$firstName</span><span class="schwarz"> ?: </span><span class="braun">'John'</span><span class="schwarz">;</span><span class="gelbgruen"> // Contains 'Anne'.</span><span class="schwarz"> </span><span class="schwarz"/></pre></div>
<p class="caption "><b>Listing 15.14</b>
Short Ternary Operator</p>
</li>
<li>
<p class="standard first-item">Quite similar to the ternary operator is the <span class="italic">null coalescing operator</span><a class="indexanchor" id="i15_73"/>. This operator is also an assignment operator in combination with <samp class="listingcharacter programmingelement">NULL</samp>, as shown in <span class="crossreference "><a href="15_003.html#l15.15">Listing 15.15</a></span>. In particular, the result of the left side does not give any hint or warning if the value does not exist.</p>
<div class="listing " id="l15.15"><pre><span class="gruen"><?php</span><span class="schwarz"><br/></span><span class="hellblau">$firstName</span><span class="schwarz"> = </span><span class="hellblau">$firstName</span><span class="schwarz"> ?? </span><span class="braun">'John'</span><span class="schwarz">;</span><span class="gelbgruen"> // Returns 'John'</span><span class="schwarz"><br/></span><br/><span class="hellblau">$firstName</span><span class="schwarz"> = </span><span class="braun">'Anne'</span><span class="schwarz">;</span><br/><span class="hellblau">$firstName</span><span class="schwarz"> = </span><span class="hellblau">$firstName</span><span class="schwarz"> ?? </span><span class="braun">'John'</span><span class="schwarz">;</span><span class="gelbgruen"> // Returns 'Anne'</span><span class="schwarz"> </span><span class="schwarz"/></pre></div>
<p class="caption "><b>Listing 15.15</b>
Null Coalescing Operator</p>
</li>
</ul>
<div class="box box_standard">
<h4 class="boxheading">The Spaceship Comparison Operator</h4>
<p class="standard first"><a class="indexanchor" id="i15_74"/>Since PHP 7, we’ve had the spaceship operator,<a class="indexanchor" id="i15_75"/> which is represented by the characters <samp class="listingcharacter programmingelement"><=></samp>. Also known as the three-way operator<a class="indexanchor" id="i15_76"/>, this operator performs a less-than-equal, a greater-than-equal, <span class="italic">and</span> an is-equal-to comparison.</p>
<div class="listing " id="l15.16"><pre><span class="gruen"><a id="p435"/><?php</span><span class="schwarz"><br/></span><span class="hellblau">$a</span><span class="schwarz"> = </span><span class="violett">5</span><span class="schwarz">;</span><br/><span class="hellblau">$b</span><span class="schwarz"> = </span><span class="violett">6</span><span class="schwarz">;</span><br/><span class="rot">echo</span><span class="schwarz"> </span><span class="hellblau">$a</span><span class="schwarz"> <=> </span><span class="hellblau">$b</span><span class="schwarz">;</span><span class="schwarz"/></pre></div>
<p class="caption "><b>Listing 15.16</b>
The Spaceship Operator in Action</p>
<p class="standard last">In our example, -1 is the output because <samp class="listingcharacter programmingelement">$a</samp> is less than <samp class="listingcharacter programmingelement">$b</samp>. If <samp class="listingcharacter programmingelement">$a</samp> were greater than <samp class="listingcharacter programmingelement">$b</samp>, (the positive number) 1 would be the output. If both values were equal, 0 would be the output.</p>
</div>
<div class="listing " id="l15.17"><pre><span class="gruen"><?php</span><span class="schwarz"><br/></span><span class="hellblau">$a</span><span class="schwarz"> = </span><span class="violett">3</span><span class="schwarz">;</span><br/><span class="hellblau">$a</span><span class="schwarz"> += </span><span class="violett">5</span><span class="schwarz">;</span><span class="gelbgruen"> // $a now has the numerical value 8</span><span class="schwarz"><br/></span> <br/><span class="hellblau">$name</span><span class="schwarz"> = </span><span class="braun">'John'</span><span class="schwarz">;</span><br/><span class="hellblau">$name</span><span class="schwarz"> .= </span><span class="braun">' Doe'</span><span class="schwarz">;</span><span class="gelbgruen"> // $name now contains 'John Doe'.</span><span class="schwarz"> </span><span class="schwarz"/></pre></div>
<p class="caption "><b>Listing 15.17</b>
Combined Operators in Action</p>
<div class="box box_standard">
<h4 class="boxheading">Note</h4>
<p class="standard first last">A good overview of all operators available in PHP, with many examples, is available at <span class="url"><a href="https://www.php.net/manual/en/language.operators.php">https://www.php.net/manual/en/language.operators.php</a></span>.</p>
</div>
</div><p class="signatur"/>
</body>
</html>/<html xmlns="http://www.w3.org/1999/xhtml" lang="en">
<head><meta content="application/xhtml+xml; charset=utf-8" http-equiv="Content-Type"/>
<meta content="application/xhtml+xml; charset=utf-8" name="content-type"/>
<meta content="Using the PHP Language" name="title"/>
<meta content="Philip Ackermann" name="author"/>
<meta content="Rheinwerk Publishing" name="publisher"/>
<meta content="© 2023 by Rheinwerk Publishing Inc., Boston (MA)" name="copyright"/>
<meta content="Full Stack Web Development - The Comprehensive Guide - Using the PHP Language" name="description"/>
<meta content="en" name="language"/>
<title>Using the PHP Language</title>
<link href="common/main.css" rel="stylesheet" type="text/css"/>
<meta content="urn:uuid:e0000000-0000-0000-0000-000030723404" name="Adept.expected.resource"/>
</head>
<body class="office_us type_kapitel">
<div id="main">
<h2 class="t2" id="h15.3">15.3 Variables, Data Types, and Operators</h2>
<p class="standard">Once MAMP has been set up, you’re ready to go. However, before we turn to a concrete example, I first want to discuss the basic programming structure<a class="indexanchor" id="i15_14"/>. For this purpose, you’ll need to create a file called <span class="italic">test.php</span> and open it in an editor of your choice. This file will be placed in the folder you specified earlier. As shown in <span class="crossreference "><a href="15_002.html#f15.1">Figure 15.1</a></span>, for me, this folder is the <span class="italic">Sites/localhost</span> directory, which is located in my user folder. Within the test file, you can specify any HTML code you like.</p>
<p class="standard">You can then define PHP areas<a class="indexanchor" id="i15_15"/> within the opening <samp class="listingcharacter programmingelement"><?php</samp> and closing <samp class="listingcharacter programmingelement">?></samp> tags, as shown in <span class="crossreference "><a href="15_003.html#l15.1">Listing 15.1</a></span>. <samp class="listingcharacter programmingelement">echo</samp> outputs “Hello World.” <span class="crossreference "><a href="15_003.html#f15.3">Figure 15.3</a></span> shows how the <span class="italic">http://localhost:8888/test.php</span> address was called in the browser. As expected, PHP returns the corresponding string.</p>
<div class="box box_standard">
<h3 class="boxheading">Opening and Closing PHP Tags</h3>
<p class="standard first last">Note that the closing PHP tags at the end of a file can be omitted. As a matter of fact, omission is even recommended (<span class="url"><a href="https://www.php.net/manual/de/language.basic-syntax.phptags.php">https://www.php.net/manual/de/language.basic-syntax.phptags.php</a></span>). Skipping the closing tag ensures that no spaces or blank lines are accidentally sent to the browser afterwards. Otherwise, an unwanted output would be created that was not intended by the programmer. Even worse, functions that depend on HTTP headers often fail, for example, sessions, cookies, or redirects.</p>
</div>
<div class="listing " id="l15.1"><pre><span class="dunkelblau"><a id="p428"/><html></span><span class="schwarz"/><br/><span class="dunkelblau"><head></span><span class="schwarz"/><br/> <span class="dunkelblau"><title></span><span class="schwarz">PHP Test</span><span class="dunkelblau"></title></span><span class="schwarz"/><br/><span class="dunkelblau"></head></span><span class="schwarz"/><br/><span class="dunkelblau"><body></span><span class="schwarz"/><br/> <span class="dunkelblau"><?php echo '<p></span><span class="gelbgruen">Hello World</span><span class="dunkelblau"></p></span><span class="gelbgruen">'; ?></span><span class="schwarz"><br/></span><span class="dunkelblau"></body></span><span class="schwarz"/><br/><span class="dunkelblau"></html></span><span class="schwarz"> </span><span class="schwarz"/></pre></div>
<p class="caption "><b>Listing 15.1</b>
Opening and Closing PHP Tags</p>
<div class="imagebox figure-type"><a href="img-f15.3.html" id="f15.3"><img alt="This test.php File Returns “Hello World” When Called in the Browser" id="img-f15.3" src="bilderklein/klein15_003.png"/></a></div>
<p class="caption "><b>Figure 15.3</b>
This test.php File Returns “Hello World” When Called in the Browser</p>
<h3 class="t3" id="h15.3.1">15.3.1 Using Variables</h3>
<p class="standard"><a class="indexanchor" id="i15_16"/><span class="crossreference "><a href="15_003.html#l15.2">Listing 15.2</a></span> shows how a variable is usually declared<a class="indexanchor" id="i15_17"/> and initialized<a class="indexanchor" id="i15_18"/>, always starting with a dollar sign<a class="indexanchor" id="i15_19"/> (<samp class="listingcharacter programmingelement">$</samp>), followed by a name (which may consist of uppercase and lowercase letters), and an underscore (<samp class="listingcharacter programmingelement">_</samp>). Numbers may also appear, but not at the beginning, or more precisely not directly after the dollar sign. In addition, PHP has some naming guidelines that you should follow (<span class="url"><a href="https://www.php.net/manual/en/userlandnaming.php">https://www.php.net/manual/en/userlandnaming.php</a></span>). A standalone declaration like in JavaScript (see <span class="crossreference "><a href="04_001.html#h4">Chapter 4</a></span>, <span class="crossreference "><a href="04_002.html#h4.2.1">Section 4.2.1</a></span>) does not exist in PHP. Likewise, the <span class="italic">casting</span>, represented in our example by the preceding <samp class="listingcharacter programmingelement">(string)</samp>, can usually be omitted since PHP automatically recognizes or defines the type of the variable.<a class="indexanchor" id="i15_20"/> (More on this topic shortly.)</p>
<div class="listing " id="l15.2"><pre><span class="gruen"><?php</span><span class="schwarz"><br/></span><span class="hellblau">$firstName</span><span class="schwarz"> = </span><span class="braun">'John'</span><span class="schwarz">; </span><span class="gelbgruen"> // Variable declaration and initialization</span><span class="schwarz"><br/></span><span class="hellblau">$firstName</span><span class="schwarz"> = (string) </span><span class="braun">'John'</span><span class="schwarz">; </span><span class="gelbgruen"> // Type casting</span><span class="schwarz"> </span><span class="schwarz"/></pre></div>
<p class="caption "><b>Listing 15.2</b>
Declaring Variables</p>
<p class="standard"><a id="p429"/>PHP offers several primitive data types,<a class="indexanchor" id="i15_21"/> listed in <span class="crossreference "><a href="15_003.html#t15.2">Table 15.2</a></span>.</p>
<table class="standardtable" id="t15.2">
<thead>
<tr>
<th class="tablehead tablecell_first top_border_cell">
<p class="standard first-item last-item">Data Type</p>
</th>
<th class="tablehead tablecell_last top_border_cell">
<p class="standard first-item last-item">Description</p>
</th>
</tr>
</thead>
<tbody>
<tr class="tablehead2">
<td class="tablehead2 tablecell_first tablecell_last top_border_cell tablecell_has_colspan" colspan="2">
<p class="standard first-item last-item">Scalar Types<a class="indexanchor" id="i15_22"/></p>
</td>
</tr>
<tr class="light">
<td class="tablecell tablecell_first">
<p class="standard first-item last-item"><samp class="listingcharacter programmingelement">bool</samp></p>
</td>
<td class="tablecell tablecell_last">
<p class="standard first-item last-item">Boolean value <samp class="listingcharacter programmingelement">true</samp> or <samp class="listingcharacter programmingelement">false</samp></p>
</td>
</tr>
<tr>
<td class="tablecell tablecell_first">
<p class="standard first-item last-item"><samp class="listingcharacter programmingelement">int</samp></p>
</td>
<td class="tablecell tablecell_last">
<p class="standard first-item last-item">Integer value (integer)</p>
</td>
</tr>
<tr class="light">
<td class="tablecell tablecell_first">
<p class="standard first-item last-item"><samp class="listingcharacter programmingelement">float</samp></p>
</td>
<td class="tablecell tablecell_last">
<p class="standard first-item last-item">Floating point number, also referred to as <samp class="listingcharacter programmingelement">double</samp></p>
</td>
</tr>
<tr>
<td class="tablecell tablecell_first">
<p class="standard first-item last-item"><samp class="listingcharacter programmingelement">string</samp></p>
</td>
<td class="tablecell tablecell_last">
<p class="standard first-item last-item">Character string</p>
</td>
</tr>
<tr class="tablehead2 light">
<td class="tablehead2 tablecell_first tablecell_last tablecell_has_colspan" colspan="2">
<p class="standard first-item last-item">Compound Types<a class="indexanchor" id="i15_23"/></p>
</td>
</tr>
<tr>
<td class="tablecell tablecell_first">
<p class="standard first-item last-item"><samp class="listingcharacter programmingelement">array</samp></p>
</td>
<td class="tablecell tablecell_last">
<p class="standard first-item last-item">A map<a class="indexanchor" id="i15_24"/> that assigns values to keys</p>
</td>
</tr>
<tr class="light">
<td class="tablecell tablecell_first">
<p class="standard first-item last-item"><samp class="listingcharacter programmingelement">object</samp></p>
</td>
<td class="tablecell tablecell_last">
<p class="standard first-item last-item">Instance of a class</p>
</td>
</tr>
<tr>
<td class="tablecell tablecell_first">
<p class="standard first-item last-item"><samp class="listingcharacter programmingelement">callable</samp></p>
</td>
<td class="tablecell tablecell_last">
<p class="standard first-item last-item">Callback function</p>
</td>
</tr>
<tr class="light">
<td class="tablecell tablecell_first">
<p class="standard first-item last-item"><samp class="listingcharacter programmingelement">iterable</samp></p>
</td>
<td class="tablecell tablecell_last">
<p class="standard first-item last-item">A pseudo-type that accepts an array or an object<a class="indexanchor" id="i15_25"/> that implements the <samp class="listingcharacter programmingelement">traversable</samp> interface</p>
</td>
</tr>
<tr class="tablehead2">
<td class="tablehead2 tablecell_first tablecell_last tablecell_has_colspan" colspan="2">
<p class="standard first-item last-item">Special Types<a class="indexanchor" id="i15_26"/></p>
</td>
</tr>
<tr class="light">
<td class="tablecell tablecell_first">
<p class="standard first-item last-item"><samp class="listingcharacter programmingelement">resource</samp></p>
</td>
<td class="tablecell tablecell_last">
<p class="standard first-item last-item">A resource<a class="indexanchor" id="i15_27"/> is a special variable that is a reference to an external resource. For example, when a file is opened for writing, the connection remains (as a resource) until it is closed.</p>
</td>
</tr>
<tr>
<td class="tablecell tablecell_first">
<p class="standard first-item last-item"><samp class="listingcharacter programmingelement">NULL</samp></p>
</td>
<td class="tablecell tablecell_last">
<p class="standard first-item last-item">Can be used to describe a variable without a value.</p>
</td>
</tr>
</tbody>
</table>
<p class="caption "><b>Table 15.2</b>
The Ten Primitive Data Types of PHP</p>
<h4 class="t4" id="h15.3.1.1">Juggling with Types (Type Juggling)</h4>
<p class="standard"><a class="indexanchor" id="i15_28"/><a class="indexanchor" id="i15_29"/>As mentioned earlier, PHP does not require or support an explicit type definition in the variable declaration<a class="indexanchor" id="i15_30"/>. The type of a variable is instead determined by the concrete value that the variable contains. In other words, if a string value is assigned to the <samp class="listingcharacter programmingelement">$var</samp> variable, <samp class="listingcharacter programmingelement">$var</samp> becomes a string. If <samp class="listingcharacter programmingelement">$var</samp> is subsequently assigned an <samp class="listingcharacter programmingelement">int</samp> value, it becomes an integer. <span class="crossreference "><a href="15_003.html#l15.3">Listing 15.3</a></span> shows how PHP proceeds.</p>
<div class="listing " id="l15.3"><pre><span class="gruen"><?php</span><span class="schwarz"><br/></span><span class="hellblau">$var</span><span class="schwarz"> = </span><span class="braun">'1'</span><span class="schwarz">; </span><span class="gelbgruen"> // $var is a string.</span><span class="schwarz"><br/></span><span class="hellblau">$var</span><span class="schwarz"> = </span><span class="hellblau">$var</span><span class="schwarz"> * </span><span class="violett">1</span><span class="schwarz">; </span><span class="gelbgruen"> // $var is now an integer.</span><span class="schwarz"><br/></span><span class="hellblau">$var</span><span class="schwarz"> = (string) </span><span class="hellblau">$var</span><span class="schwarz">; </span><span class="gelbgruen"> // $var is a string again.</span><span class="schwarz"> </span><span class="schwarz"/></pre></div>
<p class="caption "><b>Listing 15.3</b>
Type Juggling in PHP</p>
<p class="standard">To force a variable to evaluate as a specific type, you can cast it or use the <samp class="listingcharacter programmingelement">settype()</samp> function (<span class="url"><a href="https://www.php.net/manual/en/function.settype.php">https://www.php.net/manual/en/function.settype.php</a></span>). Casting (in the last <a id="p430"/>line of our example) converts the value to a specific type by writing the type in parentheses before the value to be converted. The following values are allowed:</p>
<ul>
<li>
<p class="standard first-item last-item"><samp class="listingcharacter programmingelement">(int)</samp> or <samp class="listingcharacter programmingelement">(integer)</samp></p>
</li>
<li>
<p class="standard first-item last-item"><samp class="listingcharacter programmingelement">(bool)</samp> or <samp class="listingcharacter programmingelement">(boolean)</samp></p>
</li>
<li>
<p class="standard first-item last-item"><samp class="listingcharacter programmingelement">(float)</samp> or <samp class="listingcharacter programmingelement">(double)</samp> (in PHP 7 and earlier, <samp class="listingcharacter programmingelement">(real)</samp> is also possible)</p>
</li>
<li>
<p class="standard first-item last-item"><samp class="listingcharacter programmingelement">(string)</samp> or <samp class="listingcharacter programmingelement">(binary)</samp></p>
</li>
<li>
<p class="standard first-item last-item"><samp class="listingcharacter programmingelement">(array)</samp></p>
</li>
<li>
<p class="standard first-item last-item"><samp class="listingcharacter programmingelement">(object)</samp></p>
</li>
<li>
<p class="standard first-item last-item"><samp class="listingcharacter programmingelement">(unset)</samp> (value allowed only in PHP 7 and earlier)</p>
</li>
</ul>
<p class="standard">We recommend using only the values named first, for example, <samp class="listingcharacter programmingelement">(int)</samp> instead of <samp class="listingcharacter programmingelement">(integer)</samp> because the second values are only aliases. These aliases could possibly disappear in later PHP versions, just as <samp class="listingcharacter programmingelement">(real)</samp> is no longer possible in PHP 8.</p>
<h4 class="t4" id="h15.3.1.2">References</h4>
<p class="standard">A variable does not always have to be assigned a value. Instead, you can also pass it a reference<a class="indexanchor" id="i15_31"/> by using the <samp class="listingcharacter programmingelement">&</samp> operator <a class="indexanchor" id="i15_32"/>, as shown in <span class="crossreference "><a href="15_003.html#l15.4">Listing 15.4</a></span>. A reference is therefore a pointer to another variable. If a reference is changed, the value of the referenced variable changes as well.</p>
<div class="listing " id="l15.4"><pre><span class="gruen"><?php</span><span class="schwarz"><br/></span><span class="hellblau">$lastName</span><span class="schwarz"> = </span><span class="braun">'Doe'</span><span class="schwarz">;</span><br/><span class="hellblau">$birthName</span><span class="schwarz"> = </span><span class="bold">&</span><span class="hellblau">$lastName</span><span class="schwarz">; </span><span class="gelbgruen"> // now also contains the value 'Doe'.</span><span class="schwarz"><br/></span><span class="hellblau">$birthName</span><span class="schwarz"> = </span><span class="braun">'samplewife'</span><span class="schwarz">; </span><span class="gelbgruen"> // $birthName is changed. Thereby</span><span class="schwarz"><br/></span> <span class="gelbgruen"> // also $lastName.</span><span class="schwarz"><br/></span><span class="rot">echo</span><span class="schwarz"> </span><span class="hellblau">$lastName</span><span class="schwarz">; </span><span class="gelbgruen"> // Output of $lastName: 'Deer'.</span><span class="schwarz"> </span><span class="schwarz"/></pre></div>
<p class="caption "><b>Listing 15.4</b>
Assigning a Reference</p>
<h4 class="t4" id="h15.3.1.3">Predefined Variables</h4>
<p class="standard"><a class="indexanchor" id="i15_33"/>In PHP, some variables are made available depending on the current namespace (see box). For a complete list, you should visit <span class="url"><a href="https://www.php.net/manual/en/reserved.variables.php">https://www.php.net/manual/en/reserved. variables.php</a></span>. As an example, the <samp class="listingcharacter programmingelement">$_GET</samp> variable contains all parameters given to the script via the Uniform Resource Locator (URL) <span class="italic">http://localhost:8888/test.php?firstName=John&lastName=Doe.</span> <span class="crossreference "><a href="15_003.html#l15.5">Listing 15.5</a></span> shows the output of the variables from the script shown in <span class="crossreference "><a href="15_003.html#l15.6">Listing 15.6</a></span>.</p>
<div class="listing " id="l15.5"><pre>array (size=2)<br/> 'firstName' => string 'John' (length=4)<br/> 'lastName' => string 'Doe' (length=3) </pre></div>
<p class="caption "><b>Listing 15.5</b>
$_GET Variable Now Containing the Two Values Passed by the URL</p>
<div class="listing " id="l15.6"><pre><span class="gruen"><a id="p431"/><?php</span><span class="schwarz"><br/></span><span class="violett">var_dump</span><span class="schwarz">( </span><span class="hellblau">$_GET</span><span class="schwarz"> ); </span><span class="schwarz"/></pre></div>
<p class="caption "><b>Listing 15.6</b>
var_dump() Providing a Nice Output in the Browser</p>
<div class="box box_standard">
<h5 class="boxheading">Namespace</h5>
<p class="standard first">When you define variables, functions, or classes, they are placed in the global namespace<a class="indexanchor" id="i15_34"/>. Thus, for example, you can access a constant even if the constant is defined in a completely different file. Of course, this file must be included using one of the keywords <samp class="listingcharacter programmingelement">include</samp>, <samp class="listingcharacter programmingelement">require</samp>, <samp class="listingcharacter programmingelement">include_once</samp>, or <samp class="listingcharacter programmingelement">require_once</samp> (see <span class="url"><a href="https://www.php.net/manual/en/language.control-structures.php">https://www.php.net/manual/en/language.control-structures.php</a></span>), as shown in the following examples.</p>
<div class="listing " id="l15.7"><pre><span class="gruen"><?php</span><span class="schwarz"><br/></span><span class="rot">const</span><span class="schwarz"> FOO = </span><span class="braun">"Hello World"</span><span class="schwarz">;</span><span class="schwarz"/></pre></div>
<p class="caption "><b>Listing 15.7</b>
hello-world.php file</p>
<div class="listing " id="l15.8"><pre><span class="gruen"><?php</span><span class="schwarz"><br/></span><span class="rot">include</span><span class="schwarz"> </span><span class="braun">"hello-world.php"</span><span class="schwarz">;</span><br/><span class="rot">echo</span><span class="schwarz"> FOO;</span><span class="schwarz"/></pre></div>
<p class="caption "><b>Listing 15.8</b>
test.php File</p>
<p class="standard">When <span class="italic">test.php</span> is called, the browser will output “Hello World.”</p>
<p class="standard">However, you can also define your own namespaces by using the <samp class="listingcharacter programmingelement">namespace</samp> keyword, as shown in <span class="crossreference "><a href="15_003.html#l15.9">Listing 15.9</a></span>. The <samp class="listingcharacter programmingelement">FOO</samp> constant is now no longer in the global namespace but instead in the <samp class="listingcharacter programmingelement">hello_world</samp> namespace.</p>
<div class="listing " id="l15.9"><pre><span class="gruen"><?php</span><span class="schwarz"><br/></span><span class="rot">namespace</span><span class="schwarz"> hello_world;</span><br/><span class="rot">const</span><span class="schwarz"> FOO = </span><span class="braun">"Hello World"</span><span class="schwarz">;</span><span class="schwarz"/></pre></div>
<p class="caption "><b>Listing 15.9</b>
hello-world.php File with Namespace</p>
<p class="standard">The constant can now be accessed only via an additional specification of the namespace.</p>
<div class="listing " id="l15.10"><pre><span class="gruen"><?php</span><span class="schwarz"><br/></span><span class="rot">include</span><span class="schwarz"> </span><span class="braun">"hello-world.php"</span><span class="schwarz">;</span><br/><span class="rot">echo</span><span class="schwarz"> </span><span class="bold">\hello_world\</span>FOO;<span class="schwarz"/></pre></div>
<p class="caption "><b>Listing 15.10</b>
test.php File with Access to the FOO Constant of a Different Namespace</p>
<p class="standard last">Note that the namespace must be enclosed in backslashes (<samp class="listingcharacter programmingelement">\</samp>) when referenced.</p>
</div>
<div class="box box_standard">
<h5 class="boxheading"><a id="p432"/>Scope and Variable Variables<a class="indexanchor" id="i15_35"/></h5>
<p class="standard first">Variables are only valid in certain areas, as is the case in nearly every programming language. You’ll learn more about functions in <span class="crossreference "><a href="15_005.html#h15.5">Section 15.5</a></span>, but note that variables defined in functions do not exist outside their functions. Some exceptions exist, however, and the <samp class="listingcharacter programmingelement">global</samp><a class="indexanchor" id="i15_36"/> keyword enables you to bypass this restriction. In most cases, however, global variables should be avoided, so I won’t describe them in greater detail. To learn more about this topic, as well as for an explanation of the <samp class="listingcharacter programmingelement">static</samp><a class="indexanchor" id="i15_37"/> keyword, check out <span class="url"><a href="https://www.php.net/manual/en/language.variables.scope.php">https://www.php.net/manual/en/language.variables.scope.php</a></span>.</p>
<p class="standard last"><span class="italic">Variable variables</span> are somewhat more complex<a class="indexanchor" id="i15_38"/>. To use this type of dynamic variable, detailed instructions are available at <span class="url"><a href="https://www.php.net/manual/en/language.variables.variable.php">https://www.php.net/manual/en/language.variables.variable.php</a></span>.</p>
</div>
<h3 class="t3" id="h15.3.2">15.3.2 Using Constants</h3>
<p class="standard">Constants<a class="indexanchor" id="i15_39"/> are variables that cannot be changed. Constants are defined by the <samp class="listingcharacter programmingelement">define()</samp> function, as shown in <span class="crossreference "><a href="15_003.html#l15.11">Listing 15.11</a></span>, where you can also see how the call is made, namely, <span class="italic">without</span> a preceding dollar sign.</p>
<p class="standard">Constants can also be written in lowercase, but the general convention is to use uppercase letters. Constants are always global, which means they can be accessed from anywhere, from a function as well as from a class method.</p>
<div class="listing " id="l15.11"><pre><span class="gruen"><?php</span><span class="schwarz"><br/></span><span class="violett">define</span><span class="schwarz">( </span><span class="braun">'MINUTE_IN_SECONDS'</span><span class="schwarz">, </span><span class="violett">60</span><span class="schwarz"> );</span><br/><span class="violett">define</span><span class="schwarz">( </span><span class="braun">'HOUR_IN_SECONDS'</span><span class="schwarz">, </span><span class="violett">60</span><span class="schwarz"> * MINUTE_IN_SECONDS );</span><br/><span class="violett">define</span><span class="schwarz">( </span><span class="braun">'DAY_IN_SECONDS'</span><span class="schwarz">, </span><span class="violett">24</span><span class="schwarz"> * HOUR_IN_SECONDS ); </span><span class="schwarz"/></pre></div>
<p class="caption "><b>Listing 15.11</b>
Defining Constants</p>
<div class="box box_standard">
<h4 class="boxheading">Predefined and Magic Constants</h4>
<p class="standard first last">PHP uses some predefined<a class="indexanchor" id="i15_40"/> constants/magic constants<a class="indexanchor" id="i15_41"/>. Predefined<a class="indexanchor" id="i15_42"/> constants start with a double underscore: <samp class="listingcharacter programmingelement">__FILE__</samp>, for example, returns the full path and file name of the file that’s currently being executed. You should avoid declaring constants in your projects that start with a double underscore<a class="indexanchor" id="i15_43"/> or with <samp class="listingcharacter programmingelement">PHP_</samp>, as PHP itself may one day introduce a constant with the same name. For a complete current list, refer to <span class="url"><a href="https://www.php.net/manual/en/language.constants.magic.php">https://www.php.net/manual/en/language.constants.magic.php</a></span> and <span class="url"><a href="https://www.php.net/manual/en/reserved.constants.php">https://www.php.net/manual/en/reserved.constants.php</a></span>.</p>
</div>
<h3 class="t3" id="h15.3.3">15.3.3 Using Operators</h3>
<p class="standard"><a class="indexanchor" id="i15_44"/>As in all other programming languages, many operators are available in PHP that allow you to compare or modify variables, such as the following:</p>
<ul>
<li>
<p class="standard first-item last-item"><span class="italic"><a id="p433"/>Arithmetic operators</span><a class="indexanchor" id="i15_45"/> are used for calculations. The following are available: <samp class="listingcharacter programmingelement">+</samp> (addition<a class="indexanchor" id="i15_46"/>), <samp class="listingcharacter programmingelement">-</samp> (subtraction<a class="indexanchor" id="i15_47"/>), <samp class="listingcharacter programmingelement">*</samp> (multiplication<a class="indexanchor" id="i15_48"/>), <samp class="listingcharacter programmingelement">/</samp> (division<a class="indexanchor" id="i15_49"/>), and <samp class="listingcharacter programmingelement">%</samp> (modulo<a class="indexanchor" id="i15_50"/>). In addition, these operators include the increment<a class="indexanchor" id="i15_51"/> and decrement operators<a class="indexanchor" id="i15_52"/> (<samp class="listingcharacter programmingelement">++</samp> and <samp class="listingcharacter programmingelement">--</samp>), which add or subtract 1 from a number. A power of a number can be calculated by using <samp class="listingcharacter programmingelement">**</samp>.</p>
</li>
<li>
<p class="standard first-item last-item">The <span class="italic">assignment operator</span><a class="indexanchor" id="i15_53"/> is the equal sign (<samp class="listingcharacter programmingelement">=</samp>). You’ve already encountered this operator whenever we’ve set values for variables. In addition, <span class="italic">combined operators</span> allow you to use the value of a variable in an expression and then assign the result of that expression as a new value (shown later in <span class="crossreference "><a href="15_003.html#l15.17">Listing 15.17</a></span>). These operators include <samp class="listingcharacter programmingelement">+=</samp> (for addition), <samp class="listingcharacter programmingelement">-=</samp> (for subtraction, <samp class="listingcharacter programmingelement">*=</samp> (for multiplication), <samp class="listingcharacter programmingelement">/=</samp> (for division), <samp class="listingcharacter programmingelement">%=</samp> (for modulus calculation), <samp class="listingcharacter programmingelement">**=</samp> (for exponentiation), and <samp class="listingcharacter programmingelement">.=</samp> (the string operator for concatenation<a class="indexanchor" id="i15_54"/>).</p>
</li>
<li>
<p class="standard first-item last-item"><span class="italic">Bit operators</span><a class="indexanchor" id="i15_55"/> allow you to check and manipulate specific bits in an integer. The following bit operators are available: <samp class="listingcharacter programmingelement">&</samp> (bitwise AND<a class="indexanchor" id="i15_56"/>), <samp class="listingcharacter programmingelement">|</samp> (bitwise OR<a class="indexanchor" id="i15_57"/>), <samp class="listingcharacter programmingelement">^</samp> (bitwise XOR<a class="indexanchor" id="i15_58"/>), <samp class="listingcharacter programmingelement">~</samp> (bitwise NOT<a class="indexanchor" id="i15_59"/>), <samp class="listingcharacter programmingelement"><<</samp> (bitwise shift<a class="indexanchor" id="i15_60"/> to the left), and <samp class="listingcharacter programmingelement">>></samp> (bitwise shift to the right).</p>
</li>
<li>
<p class="standard first-item"><span class="italic">Comparison operators</span><a class="indexanchor" id="i15_61"/> allow you to compare values directly. The following operators are available: <samp class="listingcharacter programmingelement">==</samp> (equal to<a class="indexanchor" id="i15_62"/>), <samp class="listingcharacter programmingelement">===</samp> (identical to<a class="indexanchor" id="i15_63"/>), <samp class="listingcharacter programmingelement">!=</samp> or <samp class="listingcharacter programmingelement"><></samp> (not equal to<a class="indexanchor" id="i15_64"/>), <samp class="listingcharacter programmingelement">!==</samp> = (not identical to), <samp class="listingcharacter programmingelement"><</samp> (less than), <samp class="listingcharacter programmingelement">></samp> (greater than), <samp class="listingcharacter programmingelement"><=</samp> (less than or equal to), <samp class="listingcharacter programmingelement">>=</samp> (greater than or equal to), and <samp class="listingcharacter programmingelement"><=></samp> (spaceship, see box). “Identical to” in PHP means not only is the value identical but also its type. Refer to <span class="crossreference "><a href="15_003.html#l15.12">Listing 15.12</a></span> for more information on this topic.</p>
<div class="listing " id="l15.12"><pre><span class="gruen"><?php</span><span class="schwarz"><br/></span><span class="hellblau">$a</span><span class="schwarz"> = </span><span class="braun">'1'</span><span class="schwarz">;</span><br/><span class="hellblau">$b</span><span class="schwarz"> = </span><span class="violett">1</span><span class="schwarz">;</span><br/><br/><span class="hellblau">$c</span><span class="schwarz"> = </span><span class="hellblau">$a</span><span class="schwarz"> == </span><span class="hellblau">$b</span><span class="schwarz">; </span><span class="gelbgruen"> // Returns true.</span><span class="schwarz"><br/></span><span class="hellblau">$d</span><span class="schwarz"> = </span><span class="hellblau">$a</span><span class="schwarz"> === </span><span class="hellblau">$b</span><span class="schwarz">;</span><span class="gelbgruen"> // Returns false because the type comparison fails.</span><span class="schwarz"> </span><span class="schwarz"/></pre></div>
<p class="caption "><b>Listing 15.12</b>
Comparisons of Equality and Identity</p>
</li>
<li>
<p class="standard first-item last-item"><a class="indexanchor" id="i15_65"/><span class="italic">Operator for program execution: Backticks</span> (<samp class="listingcharacter programmingelement">``</samp>) can be used to execute external programs. Note that backticks are not simple execution characters and that execution will fail if the <samp class="listingcharacter programmingelement">shell_exec()</samp> function has been disabled. The line <samp class="listingcharacter programmingelement">$file_list = `ls -al`;</samp> tries to execute the <samp class="listingcharacter programmingelement">ls</samp> command line tool, which lists all files and directories of the current folder.</p>
</li>
<li>
<p class="standard first-item last-item"><span class="italic">Logical operators</span><a class="indexanchor" id="i15_66"/> are for using Boolean values. The following operators can be used: <samp class="listingcharacter programmingelement">and</samp> or <samp class="listingcharacter programmingelement">&&</samp> (logical AND<a class="indexanchor" id="i15_67"/>), <samp class="listingcharacter programmingelement">or</samp> or <samp class="listingcharacter programmingelement">||</samp> (logical OR<a class="indexanchor" id="i15_68"/>), <samp class="listingcharacter programmingelement">xor</samp> (exclusive OR<a class="indexanchor" id="i15_69"/>), and <samp class="listingcharacter programmingelement">!</samp> (negation).</p>
</li>
<li>
<p class="standard first-item last-item"><span class="italic">Array operators</span><a class="indexanchor" id="i15_70"/> are used to compare or manipulate arrays. <samp class="listingcharacter programmingelement">$a + $b</samp> unites two arrays. <samp class="listingcharacter programmingelement">==</samp> returns <samp class="listingcharacter programmingelement">true</samp> if both arrays contain the same key-value pairs. <samp class="listingcharacter programmingelement">===</samp> returns <samp class="listingcharacter programmingelement">true</samp> if both arrays contain the same key-value pairs in the same order and they are of the same type. Accordingly, <samp class="listingcharacter programmingelement">!=</samp> or <samp class="listingcharacter programmingelement"><></samp> mean that two arrays are not equal. <samp class="listingcharacter programmingelement">!==</samp> in turn means “not identical,” where a type comparison is also performed.</p>
</li>
<li>
<p class="standard first-item last-item"><a class="indexanchor" id="i15_71"/><span class="italic">Type operator:</span> The <samp class="listingcharacter programmingelement">instanceof</samp> keyword checks if an object belongs to a certain class.</p>
</li>
<li>
<p class="standard first-item"><a id="p434"/>The <span class="italic">ternary operator</span><a class="indexanchor" id="i15_72"/> (shown in <span class="crossreference "><a href="15_003.html#l15.13">Listing 15.13</a></span>) is another comparison operator that returns an expression and replaces a longer <samp class="listingcharacter programmingelement">if</samp> statement. This operator also available in the short version, where the middle part can be omitted, as shown in <span class="crossreference "><a href="15_003.html#l15.14">Listing 15.14</a></span>.</p>
<div class="listing " id="l15.13"><pre><span class="gruen"><?php</span><span class="schwarz"><br/></span><span class="hellblau">$action</span><span class="schwarz"> = ( </span><span class="rot">empty</span><span class="schwarz">( </span><span class="hellblau">$_POST</span><span class="schwarz">[</span><span class="braun">'action'</span><span class="schwarz">] ) ) ? </span><span class="braun">'standard'</span><span class="schwarz"> : </span><span class="hellblau">$_POST</span><span class="schwarz">[</span><span class="braun">'action'</span><span class="schwarz">];</span><br/><span class="gelbgruen"><br/></span>// The above is identical to the IF statement below.<span class="schwarz"><br/></span><span class="rot">if</span><span class="violett"> </span><span class="schwarz">(</span><span class="rot">empty</span><span class="schwarz">(</span><span class="hellblau">$_POST</span><span class="schwarz">[</span><span class="braun">'action'</span><span class="schwarz">])) {</span><br/> <span class="hellblau">$action</span><span class="schwarz"> = </span><span class="braun">'standard'</span><span class="schwarz">;</span><br/>} <span class="rot">else</span><span class="schwarz"> {</span><br/> <span class="hellblau">$action</span><span class="schwarz"> = </span><span class="hellblau">$_POST</span><span class="schwarz">[</span><span class="braun">'action'</span><span class="schwarz">];</span><br/>} <span class="schwarz"/></pre></div>
<p class="caption "><b>Listing 15.13</b>
Ternary Operator</p>
<div class="listing " id="l15.14"><pre><span class="gruen"><?php</span><span class="schwarz"><br/></span><span class="hellblau">$firstName</span><span class="schwarz"> = </span><span class="braun">''</span><span class="schwarz">;</span><br/><span class="hellblau">$firstName</span><span class="schwarz"> = </span><span class="hellblau">$firstName</span><span class="schwarz"> ?: </span><span class="braun">'John'</span><span class="schwarz">;</span><span class="gelbgruen"> // Contains 'John'.</span><span class="schwarz"><br/></span><br/><span class="hellblau">$firstName</span><span class="schwarz"> = </span><span class="braun">'Anne'</span><span class="schwarz">;</span><br/><span class="hellblau">$firstName</span><span class="schwarz"> = </span><span class="hellblau">$firstName</span><span class="schwarz"> ?: </span><span class="braun">'John'</span><span class="schwarz">;</span><span class="gelbgruen"> // Contains 'Anne'.</span><span class="schwarz"> </span><span class="schwarz"/></pre></div>
<p class="caption "><b>Listing 15.14</b>
Short Ternary Operator</p>
</li>
<li>
<p class="standard first-item">Quite similar to the ternary operator is the <span class="italic">null coalescing operator</span><a class="indexanchor" id="i15_73"/>. This operator is also an assignment operator in combination with <samp class="listingcharacter programmingelement">NULL</samp>, as shown in <span class="crossreference "><a href="15_003.html#l15.15">Listing 15.15</a></span>. In particular, the result of the left side does not give any hint or warning if the value does not exist.</p>
<div class="listing " id="l15.15"><pre><span class="gruen"><?php</span><span class="schwarz"><br/></span><span class="hellblau">$firstName</span><span class="schwarz"> = </span><span class="hellblau">$firstName</span><span class="schwarz"> ?? </span><span class="braun">'John'</span><span class="schwarz">;</span><span class="gelbgruen"> // Returns 'John'</span><span class="schwarz"><br/></span><br/><span class="hellblau">$firstName</span><span class="schwarz"> = </span><span class="braun">'Anne'</span><span class="schwarz">;</span><br/><span class="hellblau">$firstName</span><span class="schwarz"> = </span><span class="hellblau">$firstName</span><span class="schwarz"> ?? </span><span class="braun">'John'</span><span class="schwarz">;</span><span class="gelbgruen"> // Returns 'Anne'</span><span class="schwarz"> </span><span class="schwarz"/></pre></div>
<p class="caption "><b>Listing 15.15</b>
Null Coalescing Operator</p>
</li>
</ul>
<div class="box box_standard">
<h4 class="boxheading">The Spaceship Comparison Operator</h4>
<p class="standard first"><a class="indexanchor" id="i15_74"/>Since PHP 7, we’ve had the spaceship operator,<a class="indexanchor" id="i15_75"/> which is represented by the characters <samp class="listingcharacter programmingelement"><=></samp>. Also known as the three-way operator<a class="indexanchor" id="i15_76"/>, this operator performs a less-than-equal, a greater-than-equal, <span class="italic">and</span> an is-equal-to comparison.</p>
<div class="listing " id="l15.16"><pre><span class="gruen"><a id="p435"/><?php</span><span class="schwarz"><br/></span><span class="hellblau">$a</span><span class="schwarz"> = </span><span class="violett">5</span><span class="schwarz">;</span><br/><span class="hellblau">$b</span><span class="schwarz"> = </span><span class="violett">6</span><span class="schwarz">;</span><br/><span class="rot">echo</span><span class="schwarz"> </span><span class="hellblau">$a</span><span class="schwarz"> <=> </span><span class="hellblau">$b</span><span class="schwarz">;</span><span class="schwarz"/></pre></div>
<p class="caption "><b>Listing 15.16</b>
The Spaceship Operator in Action</p>
<p class="standard last">In our example, -1 is the output because <samp class="listingcharacter programmingelement">$a</samp> is less than <samp class="listingcharacter programmingelement">$b</samp>. If <samp class="listingcharacter programmingelement">$a</samp> were greater than <samp class="listingcharacter programmingelement">$b</samp>, (the positive number) 1 would be the output. If both values were equal, 0 would be the output.</p>
</div>
<div class="listing " id="l15.17"><pre><span class="gruen"><?php</span><span class="schwarz"><br/></span><span class="hellblau">$a</span><span class="schwarz"> = </span><span class="violett">3</span><span class="schwarz">;</span><br/><span class="hellblau">$a</span><span class="schwarz"> += </span><span class="violett">5</span><span class="schwarz">;</span><span class="gelbgruen"> // $a now has the numerical value 8</span><span class="schwarz"><br/></span> <br/><span class="hellblau">$name</span><span class="schwarz"> = </span><span class="braun">'John'</span><span class="schwarz">;</span><br/><span class="hellblau">$name</span><span class="schwarz"> .= </span><span class="braun">' Doe'</span><span class="schwarz">;</span><span class="gelbgruen"> // $name now contains 'John Doe'.</span><span class="schwarz"> </span><span class="schwarz"/></pre></div>
<p class="caption "><b>Listing 15.17</b>
Combined Operators in Action</p>
<div class="box box_standard">
<h4 class="boxheading">Note</h4>
<p class="standard first last">A good overview of all operators available in PHP, with many examples, is available at <span class="url"><a href="https://www.php.net/manual/en/language.operators.php">https://www.php.net/manual/en/language.operators.php</a></span>.</p>
</div>
</div><p class="signatur"/>
</body>
</html>