PhP

  • Tutorial
  • Walk-thru
  • Quick reference
  • Examples

PHP (Hypertext Pre−Processor) is a server side HTML scripting/programming language that lets you create dynamic web pages.

Advantages:

The power of PHP is that it is cross−platform, meaning that it runs everywhere - Linux, Windows, and unix.

PHP runs 5 to 20 times faster than Java and is extremely easy to use, even when developing complex web applications.

PHP incorporates the best features from Java, C++, PERL and C.

Disadvantages:

PHP is not 100% pure Object Oriented scripting language.



      1. Always a good idea to tell your target software what kind of code it's expected to process. ;-)
        /*Start tag */
        <?php
        ...
        ...
        ?> /*End tag*/
      1. You still see so many who write code without useful comments. You still hear the old reasoning that it's a form of job security or making it more difficult for others to steal your work. Those thoughts are understandable. However, the use of relevant comments for enabling future updates with ease cannot be overstated.

        // or # for single-line comments. Everything to the end of the current line is ignored.

        /* ... */ for single- or multiple-line comments. Everything between /* and */ is ignored.

      1. Variables are containers to store values for later retrieval. Variables are a fundamental building block of any programming language. For instance, you could have a variable called number that holds the value: 6 or a variable called name that holds the value: Mary. The following PHP code declares variables with those names and values:
        $number = 6;
        $name = "Mary";
        In PHP, a variable name is always prefixed with a dollar sign, $. Use an equals symbol, =, with the variable name on the left and the value declaration on the right.

        Unlike in some programming languages, in PHP variables do not need to be declared before they can be used. You can assign a value to a new variable name any time you want to start using it.

        Variables can be used in place of fixed values. The following example uses echo to display the value stored in a variable the same way that you would display fixed text:
        $name = "Mary";
        echo "Hello, ";
        echo $name;

        Output:
        Hello, Mary
        The more descriptive your variable names are, the more easily you will remember what they are used for when you come back to a script several months afterward. Though often seen, it's not a good idea to call your variables $a, $b, and so on. How long will you remember at a glance what the variable stands for? Good variable names tell exactly what kind of value you can expect to find stored in them (for example, $price or $name).

        Variable names can contain only letters, numbers, and the underscore character, and each must begin with a letter or underscore.

        Variable names in PHP are case-sensitive. For example, $name is a different variable than $Name, and the two could store different values in the same script.

        Expressions:

        The value given does not have to be a fixed value. It could be an expression - two or more values combined using an operator to produce a result.
        $sum = 10 + 15;
        echo $sum;    //display $sum
        The variable $sum is the value of the expression to the right of the equals sign. The values 10 and 15 are combined using the addition operator, the plus symbol, +. The result of adding the two values together is returned. In this example, the value of $sum will be 25.

        Variables can be used in place of fixed values, you can perform the same addition operation on multiple variables:
        $a = 10;
        $b = 15;
        $sum = $a + $b;
        echo $sum;    //display $sum
        The values of $a and $b are added together with a displayed output of 25.

        Variables in Strings

        Text strings need to be enclosed in quotation marks and there is a difference between single and double quotes. A dollar sign in a double-quoted string indicates that the current value of that variable should become part of the string. In a single-quoted string, the dollar sign is treated as a literal character, and no reference is made to any variables.

        In this example, the value of variable $name is included in the string:
        $name = "Mary";
        echo "Hello, $name";

        This code displays Hello, Mary.
        In the next example, the dollar sign is treated as a literal, and no variable substitution takes place:
        $name = 'Mary';
        echo 'Hello, $name';

        This code displays Hello, $name.
        Sometimes you need to tell PHP exactly where a variable starts and ends. Do this by using curly brackets, {}. If you need to display within a sentence a weight value with a suffix to indicate its unit of measure:
        echo "The total weight is {$weight} lb.";
        If you did not use the braces around $weight, PHP would try to find the value of $weightlb, which does not exist.

        You could do the same thing by using the concatenation operator, the period symbol, which can be used to join two or more strings together, as shown in the following example:
        echo 'The total weight is ' . $weight . 'lb.';
        The three values two fixed strings and the variable $weight, in the order in which they should appear in the statement. Notice that a space is included at the end of the first string to provide the appropriate space before the value of $weight. If $weight has a value of 10, this statement will produce the following output:
        The total weight is 10 lb.
      1. Data types define what kind of value variables hold. When you assign a value to a variable, PHP determines the data type automatically, based on the value you assign. If you want to check what data type PHP thinks a value is, you can use the gettype function.

        Running the following code shows that the data type of a decimal number is double:
        $rate = 19.75;
        echo gettype($rate);
        Data types:
        Boolean - A truth value; can be either TRUE or FALSE.
        Integer - A number value; can be a positive or negative whole number.
        Double (or float) - A floating-point number value; can be any decimal number.
        String - An alphanumeric value; can contain any number of ASCII characters.
        The complementary function to gettype is settype, which allows you to override the current data type of a variable. If the stored value is not suitable to be stored in the new type, it will be modified to the closest value possible. The following code attempts to convert a string value into an integer:
        $value = "22nd January 2005";
        settype($value, "integer");
        echo $value;
        In this case, the string begins with numbers, but the whole string is not an integer. The conversion converts everything up to the first nonnumeric character and discards the rest, so the output produced is just the number 22. In practice, settype and gettype are seldom used. You rarely need to alter the data type of a variable.
For details, explanations, and additional info, go to Walk-thru tab.
      1. /*Start tag */
        <?php
        ...
        ...
        ?> /*End tag*/
      1. // or # for single-line comments. Everything to the end of the current line is ignored.

        /* ... */ for single- or multiple-line comments. Everything between /* and */ is ignored.

      1. In PHP, a variable name is always prefixed with a dollar sign, $.
        Use an equals symbol, = with the variable name on the left and the value declaration on the right.
        $name = "Mary";
        &rateOfPay = 19.75;
        Variable names can contain only letters, numbers, and the underscore character, and each must begin with a letter or underscore.

        Variable names in PHP are case-sensitive. For example, $name is a different variable than $Name, and the two could store different values in the same script.

      1. Remember that variables begin with $.
        Because variables are used in the connection, they must be declared and given appropriate values.
        $hostname="localhost";
        $userName="your_user_name";
        $userPassword="your_password";
        $nameOfDatabase="name_of_the_database_to_query";

        Is it required to use log-in info within a variable? No. Why do I suggest it? Details shortly.

        <?php

        //connect or quit
        mysql_connect("$hostName","$userName","$userPassword") or die(mysql_error());

        //database name required to run queries
        mysql_select_db("$nameOfDatabase") or die(mysql_error());

        ?>

        Notes:
        The nameOfDatabase is necessary to run queries.
        Also of interest is that in SQL safe mode, your_user_name is ignored and the name of the user that owns the server process is used.
      1. ---------
      1. ---------
      1. ----------
      1. --------------
      1. -----------
      1. ----------
      1. ---------
      1. ---------
      1. ----------

This section contains examples of various layouts with step-by-step instruction.



Resources

See all links
Paragon IT Consultants
Gtekk is pleased to recommend Inmotion Hosting services!






Javascript Kit - Free scripts

Adobe tutorials