Learn PHP Ez Way Preview

download Learn PHP Ez Way Preview

of 17

Transcript of Learn PHP Ez Way Preview

  • 8/14/2019 Learn PHP Ez Way Preview

    1/17

    ISBN 978-0-557-04

  • 8/14/2019 Learn PHP Ez Way Preview

    2/17

    /HDUQ3+3WKH(=:D\

    3DJHRI

    $%2877+(%22.

    *(77,1*67$57(':,7++70/

    %$6,&4$1'$6

    /($51,1*+70/ )250$77,1*7+(7(;7

    $ '',1*,0$*(6

    $ '',1*/,1.6

    3+3%$6,&6

    %$6,&4$6

    0$.(

  • 8/14/2019 Learn PHP Ez Way Preview

    3/17

    /HDUQ3+3WKH(=:D\

    3DJHRI

    Chapter

    GETTING STARTED

    WITH HTML

    1 Getting Started With HTML

    1.1 Basic Q and As:

    What is HTML?

    HTML (Hyper Text Markup Language) is a computer language devised to

    allow website creation.

    How does it Work?

    HTML consists of a series of short codes (known as tags) typed into a

    text-file by the site author. The text file is then saved as .html file, and

    viewed through a browser like Internet Explorer. The browser reads the

    text and translates it into visible form. You can use anything from notepad

    to powerful graphical editor to create web pages.

  • 8/14/2019 Learn PHP Ez Way Preview

    4/17

    /HDUQ3+3WKH(=:D\

    3DJHRI

    What does these tags do?

    These tags separate normal text from HTML code. In short, tags are

    words between for example, is a tag. These simple tags apply

    formatting to the text, look at the code below:

    This is bold text and this is not

    When the above code is saved as .html file, and opened in your browser

    then it will display:

    This is bold text and this is not

    Tags are not case sensitive, for example use of and will not

    make any difference.

    Is there anything HTML cant do?

    Yes, HTML can only make static pages (it can provide information to the

    surfer but can not take information from the visitor). For example, if you

    want the visitor to write comments about your web page, then you need to

    have a server sided languageto process this request.

    1.2 Learning HTMLThis part of the chapter will provide enough information about HTML

    language to you to be able to write PHP programs.

    An HTML web page consists of two parts, the HEAD part and the BODY

    part. HEAD part comes before BODY part.

    In the HEAD part, you define how the content in the body part of the web

    page will be displayed. HEAD part contains the title of the web page, and

    formatting of the text, which will be displayed on the body part of the web

    page.

    All HTML pages begin with and end with

    HEAD part begins with and ends with

  • 8/14/2019 Learn PHP Ez Way Preview

    5/17

    /HDUQ3+3WKH(=:D\

    3DJHRI

    BODY part begins with and ends with

    Making a basic Web page:

    Adding title of web page:

    This is my First Web page

    Above code will display a blank web page with the title

    This is my First Web page

    Now, lets come to the BODY part. The body part as discussed above

    contains the content of the web page.

    To display some text in a web page, just add the text in between the body

    tag.

    This is my First Web page

    My first Web page

  • 8/14/2019 Learn PHP Ez Way Preview

    6/17

    /HDUQ3+3WKH(=:D\

    3DJHRI

    The above code will not only display the title This is my First Web page

    but will also display the text My first Web page on the webpage.

    1.3 Formatting the text:

    Now we will do some formatting to the webpage:

    To make the text look bold, add the text in between tags.

    For Example:

    This is my First Web page

    My first Web page

    Similarly, to make the text look italic, use instead of

    You can also display the text both bold and italic by using

    similarly to underline the text use tag

    For Example:

    This is my First Web page

    My first Web page

    To make the text size larger or smaller, use the tag.

    Some text

  • 8/14/2019 Learn PHP Ez Way Preview

    7/17

    /HDUQ3+3WKH(=:D\

    3DJHRI

    Chapter

    PHP Basics

    2 PHP BASICSIn this chapter you will be able to get know how of PHP language, how it

    works and how you can make php scripts easily!

    2.1 Basic Q & As

    What is PHP?

    PHP, which stands for "Hypertext Preprocessor", is a server-side, HTML

    embedded scripting language used to create dynamic Web pages. Much

    of its syntax is borrowed from C, Java and Perl with some unique features

    thrown in. The goal of the language is to allow Web developers to write

    dynamically generated pages quickly.

  • 8/14/2019 Learn PHP Ez Way Preview

    8/17

    /HDUQ3+3WKH(=:D\

    3DJHRI

    How does PHP Work?

    In an HTML page, PHP code is enclosed within special PHP tags. When

    a visitor opens the page, the server processes the PHP code and then

    sends the output (not the PHP code itself) to the visitor's browser. It

    means that, unlike JavaScript, you don't have to worry that someone can

    steal your PHP script.

    What are the advantages of PHP over other Server Sided

    Languages?

    PHP offers excellent connectivity to many databases including MySQL,

    Informix, Oracle, Sybase, Solid, PostgreSQL, and Generic ODBC. The

    popular PHP-MySQL combination (both are open-source products) is

    available on almost every UNIX host. Being web-oriented, PHP also

    contains all the functions to do things on the Internet - connecting to

    remote servers, checking email via POP3 or IMAP, url encoding, setting

    cookies, redirecting, etc.

    What do PHP Code look like?

    PHP is a rather simple language. Much of its syntax is borrowed from C

    except for dealing with the types of variables. You don't need to think of

    the types of variables at all - you just work with their values, not their

    types. And you don't have to declare variables before you use them.

    Things to remember:

  • 8/14/2019 Learn PHP Ez Way Preview

    9/17

    /HDUQ3+3WKH(=:D\

    3DJHRI

    1. An HTML web page containing PHP code should be save as .php

    not as .html

    2. A php code starts with

    3. Each instruction in PHP code must end with semicolon (;)

    2.2 Make your first PHP Web page

    Here is a sample:

    My first PHP Script

    Save the above code in blank file as webpage.php and run this file in your

    browser, you will see the following output:

    This text is written with PHP

    As you might have noticed that the php code starts with

    To output we use echo just like in C language.

    2.3 Sending Mail with PHP

    PHP can be used to send emails, for this purpose, PHP mail() function is

    used. Many web servers have this feature enabled.

    To send email you must have a subject, a message, recipient and sender.

    Let us provide the value of each of the variables:

  • 8/14/2019 Learn PHP Ez Way Preview

    10/17

    /HDUQ3+3WKH(=:D\

    3DJHRI

    Unlike javascript, the time displayed is the time on servers PC clock.

    2.5 Checking File size

    You can check the size of any file within the directory or in any

    subdirectory.

    Lets say we want to get the size of the file, which exists in the same

    directory, lets say its name is index.html, then:

    The above function filesize() displays the size of file in bytes, you can

    convert it into Kilobytes by dividing the output by 1024.

    To round off the value to two decimal places just use the round()

    function.

    In the round function, there are two parameters:

  • 8/14/2019 Learn PHP Ez Way Preview

    11/17

    /HDUQ3+3WKH(=:D\

    3DJHRI

    round (Parameter1,Parameter2);

    Parameter 1 is the numeric value that we want to round off and

    Parameter 2 is the number of decimal places up to which the number will

    be rounded off.

    2.6 PHP if and else conditions

    PHP has support for conditional sentences. For example, if you would like

    to run different output upon different conditions then you can use if and

    else condition.

    In the following code we will display Pass if the value of a variable is

    greater than or equal to 60, and Fail if the output is less than 60.

    You can use elseif condition if there are more than two possibilities.

    For Example in the code below, we will display Positive when variable is

    greater than 60 and Neutral when variable is equal to 60 and Negative

    when variable is less than 60, so,

  • 8/14/2019 Learn PHP Ez Way Preview

    12/17

    /HDUQ3+3WKH(=:D\

    3DJHRI

    echo Neutral;

    }else{

    echo Negative;

    }

    ?>

    2.7 Sending data using Forms

    In the above examples we have declared the values within the PHP code,

    what if user wants to input the value? We use forms to let user input the

    data.

    A form code is an HTML tag which begins with and ends with

    First we create a form, which asks the user a value, and then we output

    the user whether it is positive, neutral or negative:

    PHP FORM

    Please input the value:

  • 8/14/2019 Learn PHP Ez Way Preview

    13/17

    /HDUQ3+3WKH(=:D\

    3DJHRI

    2.10 Loop

    Loops are very common in any program. Loop is a repetitive task

    assigned to script. Two most common types of loop are for and while.

    For Loop

    Here is a sample of a for loop:

    Output of above code will be:

    This is 1 line

    This is 2 line

    This is 3 line

    This is 4 lineThis is 5 line

    Same text is print five times only the line number changes each time.

    For loop consists of three parameters each separated by ; semicolon.

    Parameter 1 $i=1 is executed only once.

    Parameter 2 $i

  • 8/14/2019 Learn PHP Ez Way Preview

    14/17

    /HDUQ3+3WKH(=:D\

    3DJHRI

    echo This is $i line
    ;

    Since the value of $i is 1, so the script prints:

    This is 1 line.

    Now, the script goes to the third parameter of for loop, $i++

    It means, whatever the value of $i is, add one more to it. So now, the

    value of $i is 2. It the goes to parameter number 2, since the value of $i is

    still less than five, it again goes inside the loop and prints the line. It

    continues until value of $i becomes greater than 5 and the loop ends.

    While Loop

    While loop is similar to for loop, but it only has one parameter.

    Look at the code below:

    You can see that initial value of $i is defined outside the loop. Then while

    loops checks for the value of $i, if it is less than or equal to 5 then it goes

    inside the loop. Inside the loop, the value of $i is increased by 1, so the

    loop continues until the value of $i becomes greater than 5.

    If you do not increase the value of $i inside the loop, then loop will

    continue endlessly, and can cause your system to crash.

  • 8/14/2019 Learn PHP Ez Way Preview

    15/17

    /HDUQ3+3WKH(=:D\

    3DJHRI

    Chapter

    Using Database

    3 USING DATABASE

    3.1 Basic Q & As

    What is a Database?

    A database is simply a method by which you can store information. Lets

    say you want to store information of your employees name and address,

    you can use database for this purpose, and stored values can be viewed

    using PHP.

    What is MySQL?

    MySQL is an open source Relational Database Management System that

    uses Structured Query Language. Information is stored in "Tables" which

  • 8/14/2019 Learn PHP Ez Way Preview

    16/17

    /HDUQ3+3WKH(=:D\

    3DJHRI

    can be thought of as the equivalent of Excel spreadsheets. A single

    MySQL database can contain many tables at once and store thousands

    of individual records. It's fast, reliable and flexible.

    What is a Flat File Database?

    A Flat File database is a simple text file used to store information, and is

    most often used when you do not have access to a MySQL database, as

    they require no server addons other than PHP. They are best for smaller

    amounts of data than a MySQL database is capable of, and have fewer

    handling functions, but are very handy for things like counters and guest

    books.

    3.2 Flat File Databases

    Lets start by creating a simple raw visit counter which will count the number of

    times a page has been opened.

    In the line

    $counter_file = file(stats.txt);

  • 8/14/2019 Learn PHP Ez Way Preview

    17/17

    /HDUQ3+3WKH(=:D\

    3DJH RI

    We assign stored value of stats.txt to a variable $counter_file

    Then we read the value stored in the file, since the value is on the first

    line of the file stats.txt so it is counted as zero line in PHP!

    We make a new variable $current_stats and store the value which is on

    the first line of stats.txt in it.

    New statistics is of course 1 value additional to the old value, e.g. if it was

    4 then now it should be 5.

    Then we remove everything from the database file using the code:

    $file_open=fopen(stats.txt,w);

    and write the latest visit statistics

    fwrite($file_open,$new_stats);

    then we close the database file using:

    fclose($file_open);

    Then we output the latest visit information using:

    echo Number of Visitors: $new_stats;

    In the above example the line

    $file_open=fopen(stats.txt,w);

    opens a text file stats.txt and removes everything from it, if the file does

    not exist then it first creates a file stats.txt

    NOTE: TO RUN THE ABOVE CODE YOU MUST CREATE A TEXT FILE stats.txt

    IN THE SAME DIRECTORY IN WHICH YOU PUT THE FILE WITH THE ABOVE

    COUNTER CODE AND SET PERMISSION OF stats.txt TO CHMOD 777

    CHMOD refers to setting access privileges to a file. It is required if your web

    hosting is on Linux Server. CHMOD 777 means that a file is readable, write able

    and executable.

    To set the permission-using FTP just right click the file and select CHMOD.

    Then select all the check boxes.