WARNING AND NOTICE

All tricks in this blog are only for educational purpose. Learn these tricks only for your knowledge. Please donot try these to harm any one. We will not take any responsibility in any case. All softwares and tools on this site are here for private purposes only and If you want to use a software for business purpose, please purchase it. I do not host many of these tools. I Post links that I have found from numerous Search engines. I will not be responsible for any of harm you will do by using these tools.

Readmore

Friday, July 8, 2011

Basic PHP Syntax

This article covers basic PHP syntax, including variable usage, variable types, and several ways of printing variables to the web browser.

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. 


The basic syntax to jump in and out of PHP follows:


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 syntax

Before 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 PHP

PHP denotes all of its variables by using the $ operator followed by any combination of characters as long as these rules are followed:
  • The variable name starts with a letter or an underscore (_)
  • It is followed by any combination of letters, numbers, and underscores
Note: A letter is defined as the lowercase and uppercase characters "a" through "z" as well as any character with an ASCII value between 127 and 255 To define a variable, you can either define it with a value or by using the 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 variables

PHP 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 integer

An 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 number

Floating-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 string

A 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_one = The value of myint is $myint
  • $string_two = The value of myint is 10
Notice that, when the value of $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 characters

Next, 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.
Valid back-slashed characters
\n linefeed (LF or 0x0A in ASCII)
\r carriage return (CR or 0x0D in ASCII)
\t horizontal tab (HT or 0x09 in ASCII)
\\ backslash
\$ dollar sign
\" double-quote
\[0-7]{1,3} the sequence of characters matching the regular expression is a character in octal notation
\x[0-9A-Fa-f]{1,2} the sequence of characters matching the regular expression is a character in hexadecimal notation
John Coggeshall is a a PHP consultant and author who started losing sleep over PHP around five years ago.