Page 1 of 4 123 ... LastLast
Results 1 to 10 of 34

Thread: Want to Learn Php..?? Here is the Way...

  1. #1
    Join Date
    Jun 2008
    Posts
    342

    Default Want to Learn Php..?? Here is the Way...

    Here we are beginning a journey that reaches to Php. Feel that during this journey we are reading a book that describes the very php:

    We are not going to attempt to cover every single facet of the Wide World Of PHP Language. No, we want to grasp the basics of the language, get familiar with it, and get some real-life examples...not have the whole thing shoved down our throat!

    You might not find that web programming is the most exciting thing on the planet. Well, I don’t either, so I tried to make it interesting and easy to read.

  2. #2
    Join Date
    Jun 2008
    Posts
    342

    Default A journey leads to php....

    Presentation

    The examples in this book are presented in 3 levels - The Easy Stuff, The Not So Easy Stuff, and Oh Man Not MySQL!

    The Easy Stuff consists of common PHP commands you can use on just about any site you build. The Not So Easy Stuff is code that is a little more difficult to grasp, but you might still want to use it. Oh Man Not MySQL! will cover - you guessed it - how to connect to a MySQL database and do basic things, like writing to it and retrieving information.

    And now, without futher ado....

  3. #3
    Join Date
    Jun 2008
    Posts
    342

    Default A journey leads to php....

    Section 1

    The Easy Stuff

    (Covering ECHO, STRING, DATE, VARIABLES, INCLUDE, REQUIRE, and HEADER)

    Before we get into learning how to use PHP, we need to briefly explore what PHP is...but I’m not going to go into some long drawn out history of PHP and why it’s here. Also, it is good to be familiar with HTML before you proceed, as you will need to be able to copy and paste code in web pages.

    In the world of the wide web, there are two general types of coding languages: “Server side” and “Client side”. This means that one type is run, or interpreted, on the web server itself, and the other is run in your web browser.

    Think of it this way...
    Server side: You order a sandwich at a restaurant. It comes to you already prepared and ready to eat.
    Client side: You order a sandwich at a restaurant. Individual sandwich parts (bread, meat, lettuce, tomatoes, condiments) are delivered to you separately and you have to assemble them before you eat.


    PHP is a Server side language. All of the processing is done on the web server itself, and the result is delivered to your web browser as HTML (which, by the way, is a Client side language). Your web server must also have PHP installed in order for it to work! Most web hosting providers have PHP installed, so if you are in doubt, simply ask them.

    Things To Note:
    There are three things that need to happen before the web server correctly runs a PHP command.
    • The filename must be “file.php” instead of the usual “file.htm” or “file.html”. If it has the HTM or HTML extension, the PHP engine on the web server will ignore it. (There are exceptions to this, but we won’t go into them right now.)


    • The PHP code must be within the <?php and ?> tags. If it is not, the PHP engine on the web server will ignore it.


    • Each PHP line must end with a Semicolon. If it does not, you’re going to get some cryptic error messages.




  4. #4
    Join Date
    Jun 2008
    Posts
    342

    Default A journey leads to php....

    Now, Let’s Code!

    We’re going to tackle the basics to start with. Getting PHP to write things to the web browser, what variables are and how to use them...examples you can use in just about every web page you create.

    Let’s start with the most basic one.

    The PHP Echo command

    We’re going to tell PHP to output something to the screen. Keep in mind that PHP can be used in conjunction with HTML, but we are not showing the code in this example, to keep it simple:

    <?php echo “Creamy Bagels”; ?>

    Let’s dissect the command example bit by bit, shall we? It helps to do this when you are looking at a LOT of PHP code...because, trust me, it can look like a jumbled blurry mess sometimes if you don’t take it piece by piece! So next time, please, if you will...

    <?php - tells the server to process this as php code...

    echo “Creamy Bagels” ; - tells the server to write what’s in the quotes to the screen, and that the semicolon is ending this particular command...

    ?> - tells the webserver, “OK, I’m done with PHP for now. Back to regular HTML”.

    Pretty simple when you look at it that way, yes?

    OK, that’s cool..but what if I want to see quotes on my screen?” Want to know ? Wait....

    This can be done by “escaping” the PHP code for what you want to show up in quotes. Let’s use the example from above...

    <?php echo “Creamy Bagels”; ?>

    If you wanted to see quotes around Creamy Bagels, you would use the following code instead:

    <?php echo “\”Creamy Bagels\””; ?>

    Using \” tells PHP that you want a quotation mark to appear. Remember this - we’re going to use it later!

  5. #5
    Join Date
    Jun 2008
    Posts
    342

    Default A journey leads to php....

    Pull some Strings...

    In PHP, as a general rule, a String is any line of text contained within quotation marks. You can use either double quotation marks (“) or single quotation marks also known as apostrophes (‘) in a string. Strings could be considered the building blocks of PHP, considering most all data is going to come from what’s in a string.

    <?php
    $double = "quotation marks.";
    $single = 'single quotes.';
    ?>


    When using single quotes, you need to “escape” the apostrophe with the slash (just like you would with double quotation marks) if you wish to display it in the output text...

    <?php
    echo 'Wouldn\'t you like a bagel?';
    ?>


    Special commands within strings...

    There are some “secret commands” you can use within strings to manipulate the output text:

    \n: makes a new line
    \r: a carriage return
    \t: a tab
    \$: shows a dollar sign
    - remember PHP will be looking for a variable if you want to display a dollar sign and don’t use a slash...and throw an ugly error!

  6. #6
    Join Date
    Jun 2008
    Posts
    342

    Default A journey leads to php....

    Using Variables

    A Variable in PHP, simply put, is one thing that means another thing or things - a “container” if you will. It can represent text, numbers, calculations, and more.

    Variables are quite powerful, and if you mess ‘em up, they’ll come get you in the middle of the night.

    Declaring a variable is easy. No spaces in the variable name, please - PHP doesn’t like that...

    <?php
    $This_thing = “The Other Thing”;
    ?>


    Now before you go off saying “What the heck would I need THAT for?”, remember that variables are very useful...especially if you are PHP Include-ing other files (Like the foreshadowing there? Do ya?)

    Real World Usage For Variables

    Here’s an example of how you can use a variable in the real world: show the current date on your website.

    <?php
    $today = date("F j, Y");
    echo "$today";
    ?>


    This example sets the date command as a variable called “$today”, and uses echo to display it on the screen.

  7. #7
    Join Date
    Jun 2008
    Posts
    342

    Default A journey leads to php....

    And now, for a quick tangent...

    More about the “DATE” command - it is very versatile and flexible - see the guide below to use it to it’s potential!

    And now...back to Variables!

    The DATE command

    Expanding on the above example, here are the options for DATE and TIME display:

    Time:


    • a: am or pm
    • A: AM or PM
    • g: Hour without leading zeroes (1-12)
    • G: Hour in military time without leading zeroes (0-23)
    • h: Hour with leading zeroes (01-12)
    • H: Hour in military time with leading zeroes (00-23)
    • i: Minute with leading zeroes (00-59)
    • s: Seconds with leading zeroes (00-59)


    Days:


    • d: Day of the month with leading zeroes (01-31)
    • j: Day of the month without leading zeroes (1-31)
    • D: Day of the week abbreviations (Sun – Sat)
    • I: Day of the week (Sunday – Saturday)
    • w: Day of the week without leading zeroes (0-6)
    • z: Day of the year without leading zeroes (1-365)


    Months:


    • m: Month of the year with leading zeroes (01-12)
    • n: Month of the year without leading zeroes (1-12)
    • M: Month abbreviations (Jan – Dec)
    • F: Month names (January – December)
    • t: Number of days in the month (28-31)


    Years:


    • L: Displays 1 if it is a leap year, 0 if not
    • Y: Year in 4-digit format (2006)
    • y: Year in 2-digit format (06)
    • Other Date Formats:
    • r: Full date, including timestamp and timezone offset (O)
    • U: Number of seconds since the Unix Epoch (Jan. 1, 1970)
    • O: Offset difference from Greenwich Meridian Time (GMT). 100 = 1 hour, -100 = -1 hour

  8. #8
    Join Date
    Jun 2008
    Posts
    342

    Default


    ECHOing more that one Variable at a time


    You can use the ECHO command we learned earlier to display more than one variable at a time. Combining variables can be extremely useful. Take a look at this example...

    <?php
    $phrase1 = "That's No Moon,";
    $phrase2 = "It's a Space Station!";
    echo "$phrase1 $phrase2";
    ?>


    This code example will show up in your web browser like this:

    [IMG]file:///C:/Users/chris_2/AppData/Local/Temp/moz-screenshot-1.jpg[/IMG] That's No Moon, It's a Space Station!
    [IMG]file:///C:/Users/chris_2/AppData/Local/Temp/moz-screenshot.jpg[/IMG]
    Neat, huh?

    So, you can see where this might be useful, I hope?
    You can also echo text and variables in the same statement by putting periods around the variable....like so...

    <?php
    $items = 3;
    echo "You have purchased ".$items." items.";
    ?>


    It will display:

    You have purchased 3 items.

    PHP Includes

    A PHP include is used when you want to include the contents of one file inside another...a very useful command!

    <?php
    include("file.inc");
    ?>



    Real World Usage for Includes

    Let’s say you are developing a 5 page website that you might be adding pages to. Your navigation HTML looks like this:

    <html>
    <head>
    <title>My Navigation</title>
    </head>
    <body>
    <a href=http://www.mysite.com/index.php>Home</a>
    <a href=http://www.mysite.com/products.php>Products</a>
    <a href=http://www.mysite.com/articles.php>Articles</a>
    <a href=http://www.mysite.com/blog.php>Blog</a>
    <a href=http://www.mysite.com/contact.php>Contact Us</a>
    </body>
    </html>

    ...and this is going to be in every page. Now, when you add a page to your website, you are going to have to change the code on all 5 pages to reflect the new link. This could take several minutes to do...and what if the site expands to 20 pages or more? It’s going to be a nightmare!

    PHP Includes to the rescue!

    Turn the page and watch PHP save our developer from certain doom!

    Let’s cut the menu link code out of the example above and paste it into a new plain text document (You can use Notepad on Windows or TextEdit on the Mac for this).

    <a href=http://www.mysite.com/index.php>Home</a>
    <a href=http://
    www.mysite.com/products.php>Products</a>
    <a href=http://www.mysite.com/articles.php>Articles</a>
    <a href=http://www.mysite.com/blog.php>Blog</a>
    <a href=http://www.mysite.com/contact.php>Contact Us</a>


    Save this text file as navigation.inc. Paste the following in place of where the code was in the original HTML files...

    <?php
    include "navigation.inc" ;
    ?>


    Then save the HTML page as a PHP page. Voila! Any time you need to change the menu links, all you have to do is edit one file -
    the navigation.inc file!

    A Note on Includes...

    You don’t have to use .inc as the extension for an include -you can include almost any type of file with a PHP include - HTML, PHP, even other URL’s! One thing to keep in mind, however...is to strip out all the formatting code from the page you are including from (like the <html> and <body> tags). In other words, only include exactly what you need!


  9. #9
    Join Date
    Jun 2008
    Posts
    342

    Default A journey leads to php....

    I Require You To Include

    In PHP, you can also use the “Require” command in place of Include. The major difference between the two is that using Require will stop the script from running (the page won’t load completely) if it cannot find the page that is referenced for inclusion. An Include will allow the page to load, but it will just ignore the included code if it cannot be found.

    Now let’s mesh it all together in an example!

    Imagine this...you are building a website. Your Greatest Undertaking Of A Website. 200 Pages. 400 Articles. Writing Code By Hand. The Titan Of Websites. You want to put advertising on each page (We’ll use GoogleTM AdSense as an example). You also want to test different color themes on the ad code, or change the ads periodically. You are also going to be quite generous and give copies of this website away for others to use.

    The questions start filling your mind...how can I easily change the ad colors or format? How can I let other people easily put in their AdSense publisher code?

    We’ll use Variables and Includes to solve the problem!

    Open up 2 blank text documents. The first one is going to be a settings file, the second one is going to be your Ad code.

    In the settings file, you are going to want to set variables for things you know you are going to want to change, the main thing being the AdSense publisher code and the ad link colors. So, we’ll set the variables as below...

    <?php
    $ad_pub_num = “pub-0123456789”;
    $eb_linkcolor = “006699”;
    ?>


    The variable, $ad_pub_num, now reflects the AdSense publisher tracking code, and the link colors are the HTML color code 006699, which is a dark blue. Save this page as settings.php.

    Now, grab your AdSense code snippet from Google...

    <script type="text/javascript"><!--
    google_ad_client = "pub-0123456789";
    google_ad_width = 120;
    google_ad_height = 600;
    google_ad_format = "120x600_as";
    google_ad_type = "text_image";
    google_ad_channel = "1";
    google_color_border = "FFFFFF";
    google_color_bg = "FFFFFF";
    google_color_link = "006699";
    google_color_text = "006699";
    google_color_url = "006699";
    //--></script>
    <script type="text/javascript"
    src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
    </script>
    And paste it into the other open document...making some changes...
    <?php echo "
    <script type=\"text/javascript\"><!--
    google_ad_client = \"$ad_pub_num\";
    google_ad_width = 120;
    google_ad_height = 600;
    google_ad_format = \"120x600_as\";
    google_ad_type = \"text_image\";
    google_ad_channel = \"1\";
    google_color_border = \"FFFFFF\";
    google_color_bg = \"FFFFFF\";
    google_color_link = \"$eb_linkcolor\";
    google_color_text = \"$eb_linkcolor\";
    google_color_url = \"$eb_linkcolor\";
    //--></script>
    <script type=\"text/javascript\"
    src=\"http://pagead2.googlesyndication.com/pagead/show_ads.js\">
    </script>
    ?>

    ...and save this file as ads.php.

    Do you recognize the changes we made? We’ve told PHP to echo the code to the screen, and put in the variables that we set in the settings.php file. Since we are doing an ECHO, we’re displaying the quotation marks in the output with the \” code (That’s the way the AdSense snippet works). Now we are ready to call these files from the main web pages!
    At the beginning of your content pages, you will want to add this code:

    <?php
    require “settings.php”;
    ?>

    This tells the page that it requires the contents of Settings.php file to be included. Including this code on each page sets the variables we will use throughout the site.
    Now, call the ads in the places you want them:

    <?php
    include “ads.php”;
    ?>


    This will pull in the code from the ads page we made, including the variables we set, and show those ads wherever you care to place them. So, your ad link colors will appear in a dark blue and the example publisher number - pub-0123456789 - will show up in the ads. Name each one of your content pages as .php instead of .html, and you’ll be good to go! If you decide you want to change colors of the ads (or change the AdSense publisher ID code), you only have to make that change in ONE file instead of 200 - the settings.php file!

  10. #10
    Join Date
    Jun 2008
    Posts
    342

    Default A journey leads to php....

    Redirects using PHP HEADER

    You can use PHP to create redirects to other pages or websites. This is quite useful if you do any kind of affiliate marketing and hate the ugly links provided - use the Header command to clean ‘em up!

    <?php
    header (“Location: http://linktoredirect.com”);
    ?>


    When a viewer visits this PHP page in their browser, they will be redirected to the link reflected in the “Location” part of the code. This is a good method to use, since other redirect methods using HTML and Javascript can easily be blocked. Header can be used for other functions, but this is one of the more common uses, so we’ll leave it at that for now.


    Section One Overview...

    While we have not covered the entirety of basic PHP commands, the ones we have covered are among the most common and can be used in almost any web project.


    • ECHO: Outputs to the screen.
    • STRING: Content within a PHP command.
    • VARIABLES: Something equals something else.
    • DATE: A way to display date/time.
    • INCLUDE: Pull in the contents of another file.
    • REQUIRE: Require the contents of another file.
    • HEADER: Redirects to new file / URL.



    QUICK QUIZ!!!

    1. What is the difference between INCLUDE and REQUIRE?
    2. Can we ECHO words with quotation marks? How?
    3. What is a VARIABLE used for?

    I hope all your answers were correct.

    Very good. Now, take a short brain break...then go on to the next section!
    Last edited by film; 10-04-2013 at 11:04 AM.

Page 1 of 4 123 ... LastLast

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •