- Learning PHP 7
- Antonio Lopez
- 784字
- 2021-07-09 20:17:40
Variables
Variables keep a value for future reference. This value can change if we want it to; that is why they are called variables. Let's take a look at them in an example. Save this code in your index.php
file:
<?php $a = 1; $b = 2; $c = $a + $b; echo $c; // 3
In this preceding piece of code, we have three variables: $a
has value 1
, $b
has 2
, and $c
contains the sum of $a
and $b
, hence, $c
equals 3. Your browser should print the value of the variable $c
, which is 3.
Assigning a value to a variable means to give it a value, and it is done with the equals sign as shown in the previous example. If you did not assign a value to a variable, we will get a notice from PHP when it checks its contents. A notice is just a message telling us that something is not exactly right, but it is a minor problem and you can continue with the execution. The value of an unassigned variable will be null, that is, nothing.
PHP variables start with the $
sign followed by the variable name. A valid variable name starts with a letter or an underscore followed by any combination of letters, numbers, and/or underscores. It is case sensitive. Let's see some examples:
<?php $_some_value = 'abc'; // valid $1number = 12.3; // not valid! $some$signs% = '&^%'; // not valid! $go_2_home = "ok"; // valid $go_2_Home = 'no'; // this is a different variable $isThisCamelCase = true; // camel case
Remember that everything after //
is a comment, and is thus ignored by PHP.
In this piece of code, we can see that variable names like $_some_value
and $go_2_home
are valid. $1number
and $some$signs%
are not valid as they start with a number, or they contain invalid symbols. As names are case sensitive, $go_2_home
and $go_2_Home
are two different variables. Finally, we show the CamelCase convention, which is the preferred option among most developers.
Data types
We can assign more than just numbers to variables. PHP has eight primitive types, but for now, we will focus on its four scalar types:
- Booleans: These take just true or false values
- Integers: These are numeric values without a decimal point, for example, 2 or 5
- Floating point numbers or floats: These are numbers with a decimal point, for example, 2.3
- Strings: These are concatenations of characters which are surrounded by either single or double quotes, like 'this' or "that"
Even though PHP defines these types, it allows the user to assign different types of data to the same variable. Check the following code to see how it works:
<?php $number = 123; var_dump($number); $number = 'abc'; var_dump($number);
If you check the result on your browser, you will see the following:
int(123) string(3) "abc"
The code first assigns the value 123
to the variable $number
. As 123
is an integer, the type of the variable will be integer int
. That is what we see when printing the content of the variable with var_dump
. After that, we assign another value to the same variable, this time a string. When printing the new content, we see that the type of the variable changed from integer to string, yet PHP did not complain at any time. This is called type juggling.
Let's check another piece of code:
<?php $a = "1"; $b = 2; var_dump($a + $b); // 3 var_dump($a . $b); // 12
You already know that the +
operator returns the sum of two numeric values. You will see later that the .
operator concatenates two strings. Thus, the preceding code assigns a string and an integer to two variables, and then tries to add and concatenate them.
When trying to add them, PHP knows that it needs two numeric values, and so it tries to adapt the string to an integer. In this case, it is easy as the string represents a valid number. That is the reason why we see the first result as an integer 3 (1 + 2).
In the last line, we are performing a string concatenation. We have an integer in $b
, so PHP will first try to convert it to a string—which is "2"—and then concatenate it with the other string, "1". The result is the string "12".
Note
Type juggling
PHP tries to convert the data type of a variable only when there is a context where the type of variable needed is different. But PHP does not change the value and type of the variable itself. Instead, it will take the value and try to transform it, leaving the variable intact.
- 玩轉Scratch少兒趣味編程
- OpenCV 3和Qt5計算機視覺應用開發
- FreeSWITCH 1.6 Cookbook
- Building Mobile Applications Using Kendo UI Mobile and ASP.NET Web API
- Hands-On Microservices with Kotlin
- concrete5 Cookbook
- 深入RabbitMQ
- Kotlin從基礎到實戰
- Scala for Machine Learning(Second Edition)
- 計算機應用基礎教程(Windows 7+Office 2010)
- OpenCV Android Programming By Example
- Instant GLEW
- 例說FPGA:可直接用于工程項目的第一手經驗
- Elasticsearch搜索引擎構建入門與實戰
- Java程序設計入門(第2版)