Download presentation
1
Strings, output, quotes and comments
PHP Intro Strings, output, quotes and comments
2
Basic Syntax PHP is an HTML-embedded scripting language
Start with a basic HTML file To add PHP code, place it within PHP tags: <?php ?>
3
Example <!DOCTYPE html> <html lang="en"> <head>
<!-- File name must have php extension: first.php --> <title>First Page</title> <meta charset="utf-8"> </head> <body> Here is some text using plain HTML <br> <?php echo 'Hello PHP World'; ?> </body> </html>
4
PHP comments Three types:
# or // for single-line or end-of-line comments /* longer comments, spanning more than one line require this style of comment. */
5
PHP comments Why bother?
A reminder of what the script does (especially helpful for those who need to edit your code after you are long gone!) As a placeholder for code to be filled in later. To disable a section of code for testing and debugging purposes.
6
PHP variables Syntax rules The six PHP data types
PHP statements end with a semicolon. PHP ignores extra whitespace in statements. The six PHP data types integer double Boolean string array object
7
Data types Integer values (whole numbers)
15 // an integer -21 // a negative integer Double values (numbers with decimal positions) // a floating-point value // a negative floating-point value The two Boolean values true // equivalent to true, yes, or on false // equivalent to false, no, or off String values 'Ray Harris' // a string with single quotes "Ray Harris" // a string with double quotes '' // an empty string null // a NULL value
8
Rules for variable names
Variable names begin with $ Data types are not declared: they are determined by the PHP interpreter Variable names are case-sensitive Variable names can contain letters, numbers, and underscores. Variable names can’t contain special characters. Variable names can’t begin with a digit or two underscores. Variable names can’t use names that are reserved by PHP such as the variable named $this which is reserved for use with objects.
9
Conventions for variable names
Use meaningful names: $lastName, $userID, etc. ($s, $x, etc. don't help describe the variable and make your code hard to read) Be consistent: use camel case, or underscores, to combine words used as identifiers, typically using lower case for the first letter and then stick with that pattern throughout Variable names are case-sensitive: $first_Name is not the same variable as $first_name
10
Sending DATA to the browser
echo and print both send output to the browser technically they aren’t “functions” in the typical programming sense but they are often referred to as such echo can accept one or more string variables / print can only accept one parentheses are optional echo does not return a value / print returns a value of 1
11
PHP quotes A string must begin and end with the same type of quote
Single quotes are treated as literal strings Double quotes are interpreted: the interpreter looks inside the quotes to determine if there are variables, and if so, the value of the variable is used. If the same type of quote appears in the string, it can be escaped with \
12
String examples $first_name = 'Bob'; $last_name = 'Roberts';
Use single quotes to improve PHP efficiency $first_name = 'Bob'; $last_name = 'Roberts'; Assign NULL values and empty strings $address2 = '';// an empty string using two single quotes $address2 = null; // a NULL value Use double quotes for variable substitution $name = "Name: $first_name"; // Name: Bob $name = "$first_name $last_name"; // Bob Roberts
13
String examples (cont.)
Mix single and double quotes for special purposes $last_name = "O'Brien"; // O'Brien $line = 'She said, "Hi."'; // She said, "Hi." Or use the escape character $last_name = 'O\'Brien'; // O'Brien $line = 'She said, \'Hi.\' '; // She said, 'Hi.'
14
Using echo with strings
<!DOCTYPE html> <html lang="en"> <head><meta charset="utf-8"><title>Strings</title></head> <body> <?php // Create the variables: $first_name = 'Haruki'; $last_name = 'Murakami'; $book = 'Kafka on the Shore'; // Print the values: echo "<p>The book <em>$book</em> was written by $first_name $last_name.</p>"; ?> </body> </html>
15
Concatenation Concatenation is like addition for strings
It is the process of appending one string to another The period ( . ) is the concatenation operator for PHP $city = 'Wilmington'; $state = 'NC'; $locale = $city . $state; //what is the result? Improvement: $locale = $city . ', ' . $state;
16
Escape sequences Many characters have meanings in the PHP language, $ for example. When you want the character to be interpreted literally, it must be escaped with the \ character. Characters requiring an escape sequence: Code Meaning \" Double quotation mark \' Single quotation mark \\ Backslash \n Newline \r Carriage return \t Tab \$ Dollar sign
17
Basic debugging Don't forget to run PHP scripts through a URL
Make sure the file name has the .php extension A blank page means there is a syntax error somewhere. Check the HTML source code: run it through a validator. It may point you to the problem.
18
Debugging (cont.) Don't put comments on the first line of a page.
Use echo statements for testing purposes. Use comments to bypass sections of code for testing. This helps to isolate the problem. If you are editing locally, be sure to save and re-upload the revised file before testing.
19
Quiz yourself: Are variable names case-sensitive or case-insensitive?
What are the three types of comment syntaxes in PHP? // B. # C. /* */ Which type of quotes treat the string as a literal value? Single quotes What is the purpose of the escape character, \ ? It means that the following character should be treated as it's character code and not as it's used in the language
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.