You will learn PHP variable of type String, Numbers (Numbers), string concatenation, format the numbers, how to define constants and use the constants in PHP.
Strings
String is a collection of characters, numbers, space, and others who are in quotation marks. Below it is a string:
- January 2009 '9 '
- '1 .000.000 '
- "One million"
- 'Learn PHP'
To create a string variable are as below:
$ Name = 'Jonthan Simon';
$ Activity = 'Learn PHP';
When creating a string variable you can use quotation marks around one
or two quotes, which are important opening and closing the same. If you create a string with a single opening quotation mark the lid as well with a single quotation mark. So what if there are strings in the same character with the opening and closing for example, he says "I'm sleepy."? If you create a string like below:
$ Var = "he says" I'm sleepy. "" / / This is wrong;
then the error will occur. Writing is:
$ Var = "he says" I mengatuk "";
or may also like the following:
$ Var = 'he says "I'm sleepy." ';
To display the strings use echo () or print ():
echo $ var;
print $ date;
to display strings with the other context, use two quotation marks:
echo "Good morning, $ name";
to further deepen the understanding you do it the following example.
- Make a new document with your text editor and type the php script below:
<?php #belajar string
//membuat variabel string
$nama="Jonathan Simon";
$tgl_lahir='06 April 2008';
$komen= "Always learning PHP";
//menampilkan nilai dari variabel
echo $nama;
echo "<br>Tanggal lahir : $tgl_lahir";
echo "<br>Komentar : <br>";
echo $komen;
?>
- Save the document with the name strings.php and place it in the directory your web server. Then see the results by using your browser.
- If you do it correctly then it will be as follows:
Concatenating Strings
String concatenation is the addition operation on the strings, where the characters will be added at the end of strings. Concatenation operator is the dot (.), For example:
$ Path = 'Padma East Road no.14';
$ City = 'Kuta';
$ Address = $ path. $ City;
present value of the variable $ address is the 'Road East Padma no.14Kuta'. There is not a portrait of a clear address (usually 'Padma East Road no.14, Kuta')? therefore use the one below:
$ Address = $ path. ','. $ City;
present value of the variable $ address is the 'Padma East Road no.14, Kuta'. Concatenation works for strings and numbers, the code below will produce the same result, namely 'Padma East Road no.14, Kuta 80 361':
$ Address = $ path. ','. $ City. ' 80 361 ';
$ Address = $ path. ','. $ City. ' '. 80 361;
the result will be the same, the value of variable $ address is the 'Padma East Road no.14, Kuta 80 381'.
Numbers
The correct variable type numbers in PHP is as below:
- 2008
- 2.5
- -1
- 100 000
- 4.2e2
numbers have never been in quotation marks, and there is also no commas to indicate thousands. Numbers are assumed positive unless preceded by a minus sign (-). For digunakn decimal point mark as the separator. All the standard arithmetic operators can be used on numbers.
| OPERATOR | FACTS |
| + | addition |
| - | reduction |
| / | division |
| * | multiplication |
| % | modulus |
| + + | increment |
| - - | decrement |
There are many functions that can be used on the number, the most commonly used are the round () and number_format (). round () is used to round off numbers.
$ N = 2.25;
$ N = round ($ n); / / result is 2
round () can also be used to determine the number of decimal digits:
$ N = 10.34785;
$ N = round ($ n, 2); / / result 10:35;
number_format () is used to produce a number of commonly used formats, dividing the number in the thousands.
$ N = 10 280;
$ N = number_format ($ n); / / result is 10.280
also to determine the number of decimal digits:
$ N = 1250;
$ N = number_format ($ n, 2); / / result 1,250.00
Constants
Constants as well as a variable to store a value. The difference is a constant value can not be changed. To make the constants you can use the function define ().
define ('NAME', 'value');
the name of a constant is suggested to use a capital letter, although it does not have to. And the name of a constant is not preceded by a dollar sign. Constants can only be filled by a scalar, such as strings and numbers. Then how do I access the value of a constant? You can not access the value of a constant use of the following ways:
echo "Hello, USERNAME"; / / will not work.
This will result in Hello, USERNAME instead of USERNAME values are shown. For that you can use the following ways:
echo "Hello,";
USERNAME echo;
or use the concatenation:
echo "Hello,". USERNAME;
to further clarify I'll give an example as follows:
- Make a new document with your text editor and make the following PHP script:
<? Php # number and constant learning
/ / Create a variable number
$ Juml_brg = 20;
$ Price = 1057;
/ / Create a constant
define ('DISCOUNT', 0:35); / / 50%
/ / CALCULATING THE RESULTS
$ Total = $ juml_brg * $ price;
$ Discount = $ total * DISCOUNT;
$ Total_harga = $ total-discount;
/ / Display the results
echo 'Number of items:'. $ juml_brg;
echo "<br> Price per unit: $ price";
echo '<BR> Total:'. number_format ($ total);
echo "<br> Discount: $ discount (". (DISCOUNT * 100 ).'%)';
echo '<br> Total Price:'. number_format ($ total_harga, 2);
?>
- Save the document with the name konstanta.php and place it on your web server directory. See the results through your browser.
- If you do it correctly then it will be as follows:

Similarly Basic PHP tutorial: String, Number and Constant, in this
tutorial you have learned how to define a string, use the string in PHP,
define and use variables of type number, and how to use the constants.