2: <head>
3: <title>Listing 4.7 Defining a constant</title>
4: </head>
5: <body>
6: <?php
7: define ( "USER", "Gerald" );
8: print "Welcome ".USER;
9: ?>
10: </body>
11: </html>
Notice that we used the concatenation operator to append the value held by our
constant to the string "Welcome". This is because the interpreter has no way of
distinguishing between a constant and a string within quotation marks.
Predefined Constants
PHP automatically provides some built-in constants for you. __FILE__, for example,
returns the name of the file currently being read by the interpreter. __LINE__
returns the line number of the file. These constants are useful for generating error
messages. You can also find out which version of PHP is interpreting the script with
PHP_VERSION. This can be useful if you want to limit a script to run on a particular
PHP release.
Summary
In this hour, you covered some of the basic features of the PHP language. You
learned about variables and how to assign to them using the assignment operator.
You learned about dynamic or "variable" variables. You also learned how to assign
to variables by reference rather than by value. You were introduced to operators
and learned how to combine some of the most common of these into expressions.
Finally, you learned how to define and access constants.
Q&A
Q Why can it be useful to know the type of data a variable holds?
A Often the data type of a variable constrains what you can do with it. You may wantto make sure that a variable contains an integer or a double before using it in a
mathematical calculation, for example.
You explore situations of this kind a little further in Hour 16, "Working with Data."
Q Should I obey any conventions when naming variables?
A Your goal should always be to make your code both easy to read and understand.A variable such as $ab123245 tells you nothing about its role in your script and
invites typos. Keep your variable names short and descriptive.
A variable named $f is unlikely to mean much to you when you return to your code
after a month or so. A variable named $filename, on the other hand, should make
more sense.
Q Should I learn the operator precedence table?
A There is no reason why you shouldn't, but I would save the effort for more usefultasks. By using parentheses in your expressions, you can make your code easy to
read at the same time as defining your own order of precedence.
Workshop
The Workshop provides quiz questions to help you solidify your understanding of the
material covered. Try to understand the quiz answers before continuing to the next
hour's lesson. Quiz answers are provided in Appendix A.
Quiz
Which of the following variable names is not valid?
$a_value_submitted_by_a_user$666666xyz
$xyz666666
$_____counter____
$the first
$file-name
How could you use the string variable created in the assignment expression
$my_var = "dynamic";
to create a "variable" variable, assigning the integer 4 to it. How might you access
this new variable?
What will the following statement output?
print gettype("4");
What will be the output from the following code fragment?
$test_val = 5.4566;
settype( $test_val, "integer" );
print $test_val;
Which of the following statements does not contain an expression?
4;
gettype(44);
5/12;
Which of the statements in question 5 contains an operator?
What value will the following expression return?
5 < 2
What data type will the returned value be?
Activities
Create a script that contains at least five different variables. Populate them with
values of different data types and use the gettype() function to print each type to
the browser.
Assign values to two variables. Use comparison operators to test whether the
first value is
The same as the second
Less than the second
Greater than the second
Less than or equal to the second
Print the result of each test to the browser.
Change the values assigned to your test variables and run the script again.
No comments:
Post a Comment