PHP Introduction | Values, Variables, Arrays, and Syntax | PHP Values and Variables
Now let us modify the previous example a little more. Write it as follows and access it from a browser.
<?php
$price = 12300;
$total = $price * 1.05;
?>
<!DOCTYPE html>
<html lang="ko">
<head>
<meta http-equiv="Content-Type"
content="text/html; charset=UTF-8" />
<title>sample page</title>
</head>
<body>
<h1>Hello PHP!</h1>
<div>
<?php echo "Amount: " . $total . " won"; ?>
</div>
</body>
</html>
This time, the text becomes “Amount: 12915 won.” It calculates and displays the amount after adding 5% consumption tax to 12,300 won.
Although the source code is short, it shows several PHP characteristics. Let us roughly summarize them.
1. Scripts Can Be Split into Multiple Parts
Here, there is a <?php ?> tag at the beginning that performs the calculation, and another <?php ?> tag in the HTML body that displays the result. In this way, PHP scripts can be written in several places on a single page.
What is very interesting is that even if you write several of them, they are all recognized as a single script. For example, here the first tag performs the calculation, and the second tag displays the result. In other words, variables from the first part can be used as-is in the second part. Even if PHP scripts are split into several parts like this, they are treated as one script.
2. Variables Are Written with “$”
Here, values are stored in variables. A variable is a container that can temporarily hold a value. In PHP, variables are written as $variableName. Do not forget to put the dollar sign ($) at the beginning.
Variable names are basically written using a combination of alphanumeric characters and underscores. However, note that the first character cannot be a number.
3. Numbers Are Written As-Is, and Text Is Quoted
PHP uses various kinds of values. Numeric values can basically be written as numbers. Text must always be enclosed in quotation marks, either " or '. In this example, double quotation marks (") are used.
4. Values Are Assigned to Variables with an Equal Sign
Use an equal sign when putting a value into a variable. The equal sign plays the role of putting the value on the right side into the variable on the left side. There are other symbols for assignment, but for now, remember the equal sign first.
5. Numeric Arithmetic and Text Concatenation
When performing calculations, you can use the standard arithmetic symbols from the numeric keypad: + - * /. With these symbols, you can write ordinary expressions such as 1 + 2 - 3. If an expression is written on the right side of an equal sign, the result of calculating that expression is stored in the variable on the left. There is also a % symbol, which calculates the remainder after division.
There is also an operator for text operations. This is the dot (.), and it can be used to connect text into one string. If you write "A"."B", the result is the text "AB".
For now, understand the basics of values and variables roughly up to this point. These are truly the basics of the basics when writing scripts.