PHP $_GET Function

Posted by tutor | Posted in PHP Tutorial | Posted on 14-12-2009-05-2008

0

$_GET is a built-in function in PHP which is used to collect values in a form where the method is “get”.

Get method is rarely used though, as the information sent from a form with the GET method is visible to everyone in the browser’s address bar.

Also it has a limit on the amount of information that can be sent. All the information is concatenated in a single line and this line is printed on the browser’s address bar.

    For eg:

    <form action=”data.php” method=”post”>
    Name: <input type=”text” name=”username” />
    Address: <input type=”text” name=”address” />
    <input type=”submit” />
    </form>

When the user clicks the “Submit” button, the URL sent to the server could look something like this:

    http://www.goweb99.com/data.php?username=Tutor&address=Capital+Federal

The “data.php” file can now use the $_GET function to collect form data (the names of the form fields will automatically be the keys in the $_GET array):

    For eg:

    <html>
    <body>
    Welcome <?php echo $_GET["username"]; ?> ! to the world of PHP<br />
    You live at <?php echo $_GET["address"]; ?> .
    </body>
    </html>

Get method should not be used when sending passwords or other sensitive information.

However, because the variables are displayed in the URL, it is possible to bookmark the page, which may serve some purpose.

PHP If Condition

Posted by tutor | Posted in PHP Tutorial | Posted on 12-12-2009-05-2008

0

The If Statement

This statement executes a piece of code only when a condition is true.

Syntax

    if (condition) code to be executed if condition is true;
    For eg:

    <html>
    <body>
    <?php
    $d=date(”D”);
    if ($d==”Fri”) echo “Today is Friday!”;
    ?>
    </body>
    </html>

The if…else Statement

This is a two way condition evaluation, in which there are yes and no both lead to different code block.

We just add one else condition in the above if statement.

Syntax

    if (condition)
    code to be executed if condition is true;
    else
    code to be executed if condition is false;
    For eg:

    <html>
    <body>
    <?php
    $d=date(”D”);          if ($d==”Fri”)
    echo “Today is Friday!”;         else
    echo “Today is not Friday!”;
    ?>
    </body>
    </html>

If more than one line should be executed, then the lines should be enclosed within curly braces:

    <html>
    <body>
    <?php
    $d=date(”D”);
    if ($d==”Fri”)
    {
    echo “Hello!<br />”;
    echo “Today is Friday!”;
    }
    ?>
    </body>
    </html>

The if…elseif….else Statement

This construct is used when there are more conditions than just true or false.

When PHP evaluates If…elseif…else statement it first sees if the If statement is true. If that test comes out to be false it then checks the first elseif statement.

If then also the result comes out to be false it will check the next elseif statement, if there exists any more otherwise, it will evaluate the else segment, if one exists.

Syntax

    if (condition)
    code to be executed if condition is true;
    elseif (condition)
    code to be executed if condition is true;
    else
    code to be executed if condition is false;
    For eg:

    <html>
    <body>
    <?php
    $d=date(”D”);        if ($d==”Fri”)
    echo “Today is Friday!”;        elseif ($d==”Sun”)
    echo “Today is Sunday!”;        else
    echo “Today is neither Friday nor Sunday!”;
    ?>
    </body>
    </html>

Conditional Statements

Posted by tutor | Posted in PHP Tutorial | Posted on 12-12-2009-05-2008

0

Conditional statements are used to evaluate some conditions and perform different actions based on the outcome.

You can use conditional statements in your code to do this.

PHP provides the following conditional statements:

  • If statement – execute the piece of code only if a specified condition is true.

    if (condition) code to be executed if condition is true;

  • If…else statement – this statement executes a two way condition, where the true part does something else and the false part executes something else.

    if (condition)
    code to be executed if condition is true;
    else
    code to be executed if condition is false;

  • If…elseif….else statement – if there are multi-way condition, where there are several blocks of code to be executed based on different conditions.

    if (condition)
    code to be executed if condition is true;
    elseif (condition)
    code to be executed if condition is true;
    else
    code to be executed if condition is false;

  • Switch statement – this statement is used to select one of many blocks of code to be executed

    switch (n)
    {
    case label1:
    code to be executed if n=label1;
    break;
    case label2:
    code to be executed if n=label2;
    break;
    default:
    code to be executed if n is different from both label1 and label2;
    }

Combining Arithmetic & Assignment Operators

Posted by tutor | Posted in PHP Tutorial | Posted on 12-12-2009-05-2008

0

We often need to increment a variable by a fixed amount. For eg a variable which is being used to count something, may be a counter for a loop or some other purpose, we need to constantly increment it by 1.

    $count = $count + 1;

However, there is another way for doing this.

    $counter += 1;

This combined assignment/arithmetic operator would accomplish the same task as the above code.

Although this combined operator shortens the code but sometimes for those who are not accustomed to such programming constructs, this may create confusion and may make the code unreadable.0

    Operator Description Example Expanded Form
    += Plus Equals $x += 2; $x = $x + 2;
    -= Minus Equals $x -= 4; $x = $x – 4;
    *= Multiply Equals $x *= 3; $x = $x * 3;
    /= Divide Equals $x /= 2; $x = $x / 2;
    %= Modulo Equals $x %= 5; $x = $x % 5;
    .= Concatenate Equals $my_str.=”PHP”; $my_str = $my_str .”PHP”;

Using echo

Posted by tutor | Posted in PHP Tutorial | Posted on 11-12-2009-05-2008

0

As we used in previous pages, the PHP command echo is a way to uotput text to the web browser. Echo is the most commonly, and most ofetenly used command while working in PHP

We use echo to output a string. You can either use a variable or the string itself within quotes.

For eg:

<?php
$myString = “PHP”;
echo $myString;
echo “<h5>PHP is too good!</h5>”;
?>

Output:

PHP
PHP is too good!

The text that we output through echo is sent to the user in the form of a web page. So we can also use HTML tags inside the echo command,

Careful When Echoing Quotes!

It seems very good to be able to output HTML with PHP. However, you must be careful when using HTML code, when it contains quotes. Echo uses quotes to mark the beginning and end of the string, so you must not use quotes other than at the starting and the ending. If its necessary to use quotes inside echo you must use one of these methods

Use escape sequence to escape your quotes from echo ie just put a backslash directly before the quotation mark, like \”

Use single quotes (apostrophes) for quotes inside your string like ‘ ’.

<?php
Echo “<h5 class=”specialH5”>I Love PHP!</h5> //doesn’t work
echo “<h5 class=\”specialH5\”>I love using PHP!</h5>”; //works
?>

Echoing Variables

While echoing variables in PHP no quotations are required. They are written directly with echo as:

For eg:

<?php
$my_string = “PHP is too good  ”;
$my_number = 4;
$my_letter = U;
echo $my_string;
echo $my_number;
echo $my_letter;
?>

Output:

PHP is too good 4U

Echoing Variables and Text Strings

You can also place variables inside of double-quoted strings. This tells the PHP interpreter to output the string value of the variable to be printed in the place of the variable.

For eg:

<?php
$my_string = “PHP is too good”;
echo “$my_string to use <br />”;
echo “PHP is a scripting language $my_string <br />”;
echo “PHP is a scripting language $my_string to use”;
?>

Output:

PHP is too good to use
PHP is a scripting language PHP is too good
PHP is a scripting language PHP is too good to use

Using variables saves time and provide reusability, you don’t need to retype the whole string over and again whenever you want to use it. With variables you can just use the variable whrever theres a need to use the same variable. It makes the code readable.

You should always use double quotes with string variables, as single quotes will not give the value of the string rather it will just print the variable name like

$my_string

Using Comments in PHP

Posted by tutor | Posted in PHP Tutorial | Posted on 11-12-2009-05-2008

0

Comments are used in every programming language for making the code better to understand.

The PHP comment syntax, like other programming language, begins with a special character sequence and all text that appears between the start of the comment and the end will be ignored.

Comments are written to have better readability and understandability of the code. They provide information about the code, or sometimes omits a part of code which may not be used at present ar for a particular scenario like for testing.

A comment’s main purpose is to serve as an informative note to you, the web developer or to others who may view your source code.

PHP’s comments are not displayed to the visitors visiting the website, even if they view the page’s source. The only way to view PHP comments is to open the PHP file for editing. So for this reason, the PHP comments are useful only for the developers, unlike HTML, where the visitor can also view the comments.

PHP Comments is of two types

  • Single line – // or # followed by the line to be commented out.
  • Multiple lines – /* */ is used to mark the beginning and the end of multiple lined comment.

PHP Comment Syntax: Single Line Comment

The single line comment tells the interpreter to ignore everything that occurs on that line to the right of the comment. To do a single line comment type “//” or “#” and all text to the right will be ignored by PHP interpreter.

For eg:

<?php
echo “It’s so good to be here!”;                    // This will print out It’s so good to be here!
echo “<br />There are comments on this page but you can’t see them…!!!”;
// echo “This is not for you”;
// echo “My name is Tutor!”;
# echo “I am the PHP programmer”;
?>

Output:

It’s so good to be here!
There are comments on this page but you can’t see them…!!!

Some of our echo statements were not evaluated because we commented them out with the single line comment.

This type of line commenting is often used for quick notes about complex and confusing code or to temporarily remove a line of PHP code, may be for testing.

PHP Comment Syntax: Multiple Line Comment

The multi-line PHP comment can be used to comment out multiple lines of code or writing multiple line comments. The multiple line PHP comment begins with ” /* ” and ends with ” */ “.

For eg:

<?php
/* This Echo statement will print out my message to the
the place in which I reside on. In other words, the World. */
echo “It’s so good to be here!”;
/* echo “My name is Tutor!”;
echo “I am the PHP programmer!”;
*/
?>

Output:

It’s so good to be here!

Good Commenting Practices

The best commenting practice is to use them, anywhere and everywhere, where there is a need to put some extra detail, so that everyone viewing the code can atleast understand the structure.

Use single line comments for quick notes about a tricky part in your code and use multiple line comments when you need to elaborate on some function or some construct.

PHP – How to Start With?

Posted by tutor | Posted in PHP Tutorial | Posted on 11-12-2009-05-2008

0

Every language has some rules to how to write them or all languages has some defined words which must be used to make a meaningful sentence. Like that PHP too has some rules that must be followed to write properly structured code, which could be understood by the PHP interpreter.

PHP is similar to most of the other programming languages (C, Java, and Perl) in syntax and semantics.

All PHP code is contained within a tag. As PHP is less often used stand alone so all the PHP code is written within a tag which marks the beginning and the end of PHP code.

<?php
//Your PHP code goes here
?>

Or you can use the shorthand

<?
//Your PHP code goes here
?>

However, if you are using multiple scripting languages then you must use the former to avoid confusion and mixing of all the codes.

PHP code written outside this tag will not be interpreted and will be directly displayed on the browser as some jumbles.

A PHP block can be anywhere in your page.

Saving Your PHP Pages

A PHP file is a file with extension “.php”, “.php3″, or “.phtml”. It can be written in any SDI (Single Document Interface) like notepad, and save with any of the three extensions.PHP files can contain text, HTML tags and scripts.

PHP scripts runs on the server, when a browser sends request PHP files are returned to the browser as plain HTML

If you have inserted PHP into your HTML page, then you must save the file with a .php extension, instead of the standard .html extension, then only it will be interpreted as some script otherwise it will be displayed as it is.