Results 1 to 4 of 4

Thread: [Tutorial] PHP Intro (More Coming Soon)

  1. #1

    Thread Starter
    WiggleWiggle dclamp's Avatar
    Join Date
    Aug 2006
    Posts
    3,527

    [Tutorial] PHP Intro (More Coming Soon)

    Pre-Info
    Before you start coding php, you are going to need the following:
    • PHP enabled server
    • A Text Editor or DreamWeaver.


    Lets Begin

    PHP is a language that is embedded into HTML code. PHP is translated into HTML before it reaches the user in their browser.

    Example:

    PHP
    PHP Code:
    <html>
    <body>
    <?PHP echo "Hello World"?>
    </body>
    </html>
    HTML
    PHP Code:
    <html>
    <
    body>
    Hello World
    </body>
    </
    html

    PHP Syntax

    When ever you want to use php code, the code must be inside "<?PHP" & "?>" tags.

    Also, every line in PHP must has the semi-colon ( at the end of the line.

    Example

    PHP Code:
    <?PHP
    echo "Hello World!<br>";
    ?>

    //Displaying text w/ shorthand tags:
    <?="Hello World"?>
    Browser Output:
    Hello World!
    Hello World!


    Comments

    Like in HTML and many other programming languages, you can have a comment within your code. there are many forms of a comment.

    Example

    PHP Code:
    //One line comment example

    /*
    Block Commenting
    some more of the comment 
    some more
    some more
    end of comment
    */ 
    Variables - Strings

    A Variable can have either Strings (text) or integers (numbers), and are shown with either a name, or a letter.

    A variable always begins with "$" sign and then the name of the string, can be name you want
    PHP Code:
    //Create A String
    $string "Hello World";

    //Display a String
    echo $string
    Browser Output:
    Hello World

    Concatenate
    To concatenate 2 strings together you use the . (dot) operator

    PHP Code:
    $name "Dylan";
    $message "Hello: ";
    echo 
    $message $Name
    Browser Output:
    Hello Dylan

    You can also have text and a string concatenated together. We are going to use the date() function to show the time, i will get into this more in a later tutorial.

    PHP Code:
    $name "Dylan";
    $time date(g:i);
    echo 
    "Hello " $name ", It is " $time
    Browser Output:
    Hello Dylan, It is 11:23

    String Naming Rules
    • Dont use spaces in the name, use underscore (_) or hyphen (-)
    • It is not recommended that you named your string in all UPPERCASE
    • Choose names that are related to what it contains.


    (More on Strings coming soon.)

    Variables - Integers
    Here i am going to show you how to work with Integers within Variables.

    PHP Code:
    //Create and Integer
    $integer "1";
    echo 
    $integer
    Browser Output:
    1

    Adding Integers
    Adding to an integer is easy, you are going to want to have an integer with a value, lets say 1, and then we are going to add 1 to it.

    PHP Code:
    //There are 2 ways of doing this, the recommended way:
    $int "1";
    $int $int++;
    echo 
    $int;

    //Another Way, This way is also acceptable:
    $int "1";
    $int $int "4";
    echo 
    $int

    More on integers coming soon...


    EDIT: Thanks Kows and Penagate for corrections!
    Last edited by dclamp; Apr 12th, 2007 at 07:31 PM.
    My usual boring signature: Something

  2. #2
    PowerPoster
    Join Date
    Sep 2003
    Location
    Edmonton, AB, Canada
    Posts
    2,629

    Re: [Tutorial] PHP Intro (More Coming Soon)

    first, I don't think saying MySQL is required is accurate at all (especially considering you even said it yourself that it's not required for this tutorial; well, if it isn't required for your PHP tutorial, why would it be required to program the language?). you should probably change that before you mislead people. just about any type of database is supported by PHP, and you are not required to have access to any.

    the short hand PHP tags are actually <? and ?>. the equals sign is short for echoing one string of text; while if you used the equals sign to try to do anything, it would not parse.
    PHP Code:
    shorthand echo:
    <?="shorthand"?>
    longhand echo:
    <?php echo "shorthand"?>
    shorthand execution:
    <? $i++; myFunction($i); ?>
    longhand execution:
    <?php $i++; myFunction($i); ?>
    moreover, you should not encourage anyone at all to use shorthand any longer. it is a good habit to get out of now if you are still using it. I suppose if you never use XML, you might not care; but just to be safe you should always be using the longhand PHP openings ("<?php" and "?>"). I think shorthand has been depreciated officially, too.

    your bit about strings and integers is a little mixed up. when you say a string can be a number or text, that's a little misleading. you can't do mathematical equations with strings. you can do mathematical equations with integers. what you might have meant was that VARIABLES can hold strings or numbers, and variables are identified by the scalar ("$"). I have no idea who told you not to use uppercase letters in a string, but they're morons. you probably shouldn't use COMPLETE uppercase; but having uppercase characters is not against the "rules."

    you don't define an integer by using quotes around it. quotes denote a string; and an integer simply isn't a string. I don't know how much it matters to the parser, but this is more "proper":
    PHP Code:
    $i 1;
    $i++;
    print 
    $i;
    /* will print:
     * 2
     */ 
    also, your request to display the time (under strings again) will not run. the time() function has no parameters; and g/i are not constants that you've defined (and the whole colon thing would make the script crash, anyway). you meant to use date(), and the string "g:i" instead.
    PHP Code:
    $name "Dylan";
    $time date("g:i");
    echo 
    "Hello " $name ", It is " $time
    another thing, appending onto a string the way you're doing it is a little useless (if you ask me), simple because double-quoted strings can hold variables. if you were using single-quoted strings it would make more sense. for example:
    PHP Code:
    //no concatenation
    echo "Hello $name, it is $time";
    //needed concatenation, unless you desire literal printing
    echo 'Hello ' $name ', it is ' $time
    and one last note for now: at the beginning of your tutorial, you should display the PHP code first, and then display what the output HTMl would look like. it makes more sense, in that you are showing that the PHP code is parsed into HTML, rather than what you have now (HTML that looks like it's translating into PHP code).

    ps: many, many, many spelling errors.

  3. #3
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: [Tutorial] PHP Intro (More Coming Soon)

    Short tags are deprecated and (I think) won't be supported in PHP 6. Also, it's usually written <?php, not <?PHP.

    Quote Originally Posted by kows
    I have no idea who told you not to use uppercase letters in a string, but they're morons. you probably shouldn't use COMPLETE uppercase; but having uppercase characters is not against the "rules."
    I think dclamp's "rules" are more style convention. In which case, I agree (as a matter of opinion), but you can't use hyphens in a variable name.

    Good start though, keep working on it.

  4. #4
    Hyperactive Member
    Join Date
    Dec 2006
    Location
    Ubuntu Haters Club
    Posts
    405

    Re: [Tutorial] PHP Intro (More Coming Soon)

    No, shorthand tags are staying in PHP6, ASP Style tags are going.

    (Just like Register Globals - Yippeeeeeee!)
    » Twitter: @rudi_visser : Website: www.rudiv.se «

    If Apple fixes security flaws, they are heralded as proactive. If Microsoft fixes a security flaw, they finally got around to fixing their buggy OS.

Posting Permissions

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



Click Here to Expand Forum to Full Width