Chapter 9 PHP 9.2
echo "9 \$x"; $x = 12;
9 $x \$ outputs a dollar sign in a double-quoted string
PHP code runs in a web browser. (t/f)
False PHP code runs on a web server. JavaScript runs in a web browser.
variable is illegally named
$2b_ Variables may not start with a digit after $.
constant is illegally named?
$A1_ Constants may not begin with a $.
code segment produces a notice error message
$count = 2; echo $COUNT; PHP variables are case-sensitive, so $COUNT is an uninitialized variable.
Which statement produces a parse error?
$owner = 'Pam's'; A single quote in a string delimited with single quotes must be escaped with a backslash: $s = 'Pam\'s';
What file extension does a PHP script have?
.php
echo '9 $x 0'; $x = 12;
9 $x 0 Single-quoted strings do not perform variable interpolation, so $x remains in the output.
echo '9 ' . $x; $x = 12;
9 12 $x is concatenated to the back of "9" and a space.
echo "9 $x 0"; $x = 12;
9 12 0 $x's value is substituted into the double-quoted string.
echo '9 \\'; $x = 12;
9 \ \\ is converted to a single backslash in single and double-quoted strings.
echo "9{$x}0"; $x = 12;
9120 Curly braces around a variable ensure proper variable interpolation.
What output is produced by the PHP script? <h1>This is a test</h1> echo "This is only a test";
<h1>This is a test</h1> echo "This is only a test"; The <?php and ?> tags are missing, so the PHP code is treated as HTML and output as-is.
Warning E_WARNING - warning
Runtime error that does not halt execution of the script. Ex: Dividing by zero. $answer = 2 / 0; Division by zero causes a warning.
Fatal error E_ERROR - fatal error
Runtime error that halts execution of the script. Ex: A loop that causes the script to exceed the maximum execution time (30 seconds by default). while (TRUE); A while loop that never terminates causes the script to execute too long.
Parse error E_PARSE - parse error
Syntax error generated when the script is compiled. Ex: Misspelling "echo" or forgetting a semicolon at the end of a statement. echo $answer A missing semicolon at the end of a statement causes a parse error.
Many PHP applications run on Linux with an Apache web server and MySQL as the database. (t/f)
True LAMP (Linux, Apache, MySQL, PHP) is a popular bundling of technologies used to deploy many web applications.
PHP is used by Facebook, Wikipedia, and WordPress. (t/f)
True PHP is nearly as old as the Web, but PHP continues to remain a popular server-side technology, even for large websites.
echo "9$x0"; $x = 12;
Warning No variable called $x0 exists, and the PHP engine outputs a warning message when outputting a variable that is not initialized.
Must PHP statements be terminated by a semicolon? (yes/no)
Yes Like many programming languages, PHP statements end with a semicolon.
PHP script
a file composed of HTML tags and PHP code inserted between <?php and ?> tags. When the browser requests a PHP script, the web server starts the PHP engine The PHP script uses the echo statement to produce output that is sent to the browser in an HTTP response.
comment
a message intended for a human that is ignored by the PHP engine. The 3 ways to create comments are shown in the figure below. // Single line comment # Single line comment /* Multiple line comment */
variable
a named container that stores a single value. Unlike many programming languages, no statement exists to declare a variable in PHP; initializing a variable creates the variable Variable names must start with a $ sign, can be any combination of letters, digits, or underscores, may not start with a digit after $. case-sensitive
PHP
a popular server-side scripting language used to create web applications. Rasmus Lerdorf created PHP in 1994 and named his creation Personal Home Page. PHP has evolved over the years and now stands for PHP: Hypertext Preprocessor.
What does the code output? $test = "adios"; echo "$test test";
adios test The $test variable is output as "adios".
constant
an identifier initialized to a value that does not change. Constants are like variables except: cannot be changed do not use $ in the name are case-sensitive are usually CAPITALIZED
String concatenation
appends one string after the end of another string, forming a single string. Ex: $x = "test"; $y = "This is a" . $x; // "This is a test"
PHP engine
compiles the requested PHP script and executes the code
statement is equivalent to const EMAIL = "[email protected]";
define("EMAIL", "[email protected]"); One difference between const and define() is that define() can create a constant name from a variable, and const cannot. Ex: define("NAME" . $i, "Jack"); defines a constant NAME5 if $i is 5.
phpinfo()
displays information about PHP's configuration
Notice E_NOTICE - notice
echo $uninitializedVariable; Outputting an uninitialized variable generates a notice.
The var_dump(variable) function outputs the data type of a variable. What is the output of the code below? $x = 1.23; var_dump($x)
float(1.23) 1.23 has a decimal place, so 1.23 is a floating point number
Variable interpolation
is when the PHP engine substitutes the value of a variable for the variable name inside of a double-quoted string. Ex: $x = "test"; $y = "This is a $x"; // "This is a test" Single quotes around a string turn off variable interpolation. Ex: $x = "test"; $y = 'This is a $x'; // "This is a $x"
php.ini
php.ini file contains configuration settings. When writing and debugging PHP code, most developers enable error reporting (error_reporting = 1) in php.ini to generate four types of error messages: Parse error, Fatal error, Warning, Notice
PHP data types
string, integer, float, boolean, array, object, null, resource
PHP 7
the latest version of PHP that adds many new features and functions to the language and increases execution speed. Some PHP 5 features are not supported in PHP 7