إعلانيمكن أن تكون علامتك التجارية هناعلامتك التجارية هنافي كل صفحة من Darslab · طلاب وأولياء أمور ومعلمونinfo@darslab.comاسأل عن الأسعارالأسعارinfo@darslab.com
☰ خطة العام · تكنولوجيا المعلومات والاتصالات (بالإنجليزية)

PHP basics: echo, variables and constants

الأسبوع 10 · الوحدة ٣ «إنشاء صفحات الموقع» (٣) الموضوع الأول (تابع): كود PHP في الصفحة، echo وprint، التعليقات، المتغيرات وعلامات الاقتباس والأنواع، الثوابت · 3 دروس
2

Variables, quotes and types

ICT Secondary 2 Term 1, Unit 3, Topic 1: pp. 78–82
GoalUse variables, tell the three quote styles apart, and name the main data types.
New words
variable · a named place that holds a value which may change; its name starts with $assignment · putting a value into a variable with =double and single quotes · double quotes print a variable’s value; single quotes print the text as writtendata type · string, integer, double or boolean: the kind of value a variable holds
Explanation

A variable is a named place for a value that may change. Its name starts with a dollar sign, then letters, digits or underscores: $name, $books_read. The sign = does not mean “equals”; it assigns: it puts the value on the right into the variable on the left. You need not declare a variable first; PHP decides its type from the value. Variable names are case-sensitive, so $total and $Total are two different variables. Quotes matter. In double quotes PHP replaces a variable by its value; in single quotes it prints the text exactly as written. To join pieces use a dot. The main types are string (text), integer (a whole number), double (a number with a decimal part) and boolean (true or false). The function gettype tells you the type. If a variable has no value yet, give it one yourself, for example $x = null; reading a variable that was never set gives a warning.

TypeExamplegettype prints
text"Mona"string
whole number123integer
decimal number1.23double
true or falsetrueboolean
no valuenullNULL
Worked examples
CodeWhat the page shows
echo "my name is $name";my name is Mona
echo 'my name is $name';my name is $name
echo 'my name is ' . $name;my name is Mona
The same variable printed in three ways. The first echo uses double quotes, so the value appears. The second uses single quotes, so the text stays as written, with $name. The third joins the pieces with a dot.
Php
1<?php
2$books = 3;
3$books = 5;
4$total = 5;
5$Total = 9;
6echo "Books: " . $books . " " . $total . " " . $Total;
7?>
Output
Books: 5 5 9
A variable can change: the second assignment replaces the first value. Variable names are case-sensitive: $total and $Total are two different variables with two values.
Php
1<?php
2echo gettype("Mona"), " ", gettype(123), " ", gettype(1.23), " ", gettype(true), " ", gettype(null);
3?>
Output
string integer double boolean NULL
gettype tells the type that PHP gave to each value. Notice that it prints double for a number with a decimal part, and NULL in capitals for no value.
Class activity

On paper write a PHP block with three variables for a club member (a name, a number of books and whether the card is active). Print one sentence that uses all three, once with double quotes and once with the dot. Then write which gettype word each variable gives.

Practice
1
What is the value of $b after: $a = 4; $b = $a; $a = 7; ?
2
Why are $total and $Total two variables?
3
What does echo 'Hi $name'; print, when $name is "Mona"?
4
Which type does gettype report for 123, and which for 1.23?