Embedded code blocks:
PHP is an embedded web development language with many similarities to commercial packages such as
Microsoft's Active Server Pages (ASP) or Cold Fusion. One of the similarities between PHP and these packages (especially ASP) is the ability to jump between PHP and HTML code quickly and easily.
My first
This is normal HTML code
Back into normal HTML
In this example, we see that PHP code is signified by the use of the to begin the PHP block and ?>
to signify the end of the code block. Although this a completely acceptable method of encapsulating your PHP code there are many other ways that are all, as far as syntax is concerned, correct. Here are some other ways to mark PHP code blocks.
Valid syntax to indicate a PHP code block | ||||||||||||||||||
| Standard PHP syntax | |||||||||||||||||
| Shorthand version | |||||||||||||||||
<% ... %> | ASP-style PHP syntax | |||||||||||||||||
Back into normal HTML General syntaxBefore we discuss variables, we should begin with some general PHP syntax rules. First, all single-line statements must conclude with a semicolon. In addition, statements that exceed a single line (such as most conditionals) must be surrounded by{ and } characters. Finally, the double forward slash (// ) represents a comment and everything past those characters until the end of the line will be ignored by PHP. Now, on to the variables in PHP!Variables in PHPPHP denotes all of its variables by using the$ operator followed by any combination of characters as long as these rules are followed:
var operator. Example: In this example, the variable $myvar is assigned the string value "foo" while the second variable we defined $my_second_var is empty and contains nothing. For a comparison, we will now give you some examples of invalid PHP variables: Now that we have learned proper syntax for our variables, we can move on the variable types and functions.Types of variablesPHP is what is known as a "loosely typed" language. What that means is any given variable can be an integer, floating-point number, string, object, or an array. In this article, we will only be discussing the first three types.Type 1: The integerAn integer is the basic mathematical datatype and represents any whole number and usually can be any value from minus 2 billion to 2 billion. When assigning an integer value, three different types of notation can be used: decimal (regular base 10), hexadecimal (base 16), or octal (base 8). Usually, only the normal decimal notation is used but there are special cases when a hexadecimal or octal notation makes life easier for the developer.
Type 2: The floating-point numberFloating-point numbers are the second mathematical datatype PHP provides. A floating-point number represents any value that contains a decimal point. Floating-point numbers are somewhat unreliable in the sense that the value stored in them is not always the exact value the developer expects, but for now we will ignore that. Instead, we will focus on the notation used to assign a variable a floating-point value. Floating-point numbers can be expressed in two different types of notation: decimal and scientific.
Type 3: The stringA string is a datatype we first used in our original examples to assign values to variables. A string can be any combination of letters, numbers, or special symbols as long as consideration is given to characters that have functions in PHP. Before we consider special cases, let's first discuss the difference between the two string notations: the single and double quote. In every case where you may want to assign a string value to a variable, the value itself must begin and end with a pair of either single (' ' ) or double (" " ) quotes. In this example, both variables would simply be assigned a value within the single or double quotes. However, when double quotes are used, PHP will first look inside the string for any references to variables that may exist. If any references are found, they are replaced with values before being assigned to the designated variable. Conversely, when dealing with single-quoted strings, PHP simply takes the string as-is and assigns it to the designated variable. Consider the above example. In the first line, we simply assign the integer value "10" to the variable $myint . Then, we assign two more variables $string_one and $string_two . These are identical except $string_one is stored using single quotes and $string_two is stored using double quotes. In this example, the values within the two strings are as follows:
$string_two is displayed, the variable $myint was replaced with the value "10". In the single-quoted string, however, the actual string $myint was stored.Special charactersNext, we will discuss "special" characters. Here's a common scenario: You are developing a web site and find yourself needing to store the double-quote character itself (" ) as a string within another variable. You can't simply place the double quote within a set of double quotes because it will cause an error in PHP. To overcome this dilemma, a method called an "escape" is used to allow developers to store this special character along with others in strings. To escape a character, the character is simply prefixed by the backslash character (\ ). So, to store the double-quote character in a string, we would instead tell PHP to store the string \" instead. Here is an example: The first attempt is incorrect due to the use of "un-escaped" double quotes within the string itself. The proper syntax for storing this string is illustrated in the second example where the quotes surrounding the word "escape" are properly coded. Below is a table of other special characters that require a backslash.Note: Although attempting to store a un-escaped double-quote within a string will cause an error, storing a single-quote within a double-quoted string will not throw an error and is completely acceptable.
|