Results 1 to 6 of 6

Thread: [PHP] Echoing, Variables and Introduction to Functions

  1. #1
    Fresh Paint
    Join Date
    Jun 2008
    Location
    Adelaide
    Posts
    29

    Default [PHP] Echoing, Variables and Introduction to Functions

    For anyone whos interested in learning php:

    Righto, When your coding php it's handy to have some form of php editor. It makes things easier to read and what not, I use Adobe Dreamweaver CS3. You are also going to need somewhere to test your php scripts, so get a free webhosting account or set up a local web server using WAMP or XAMPP (google on or the other )

    Where do i start?
    When you start to write php, you need to put your php tags onto the page so the browser will recognize it as php.

    To do this, all ya gotta do it write what is in the code box below, and your set to start writing php.

    Code:
    <?php
    
    *code goes here*
    
    ?>
    Wtf is echoing?
    Usually echoing is when sounds reflects off walls, therefore repeating. But thats got nothing to do with echoing in php.

    If you echo something in php it basically means your outputting it onto the page.

    Below is the code for a simple hello world page. Read the comments to see what what does. (a comment is what php will ignore, anything on the same line after // is commented out)

    Code:
    <?php // Opening php tag.
    
    echo("Hello World"); // The page will display the text "Hello World"
    
    ?> // Closing php tag.
    That was easy, whats next?
    Variables.

    Variables are little 'things' that data is stored in. Look at the different types of code below for a few examples of how variables can be used.

    Code:
    <?php
    
    $variable = "Hello World";
    
    echo($variable); // Outputs the text "Hello World"
    
    ?>
    Code:
    <?php
    
    $variable = 1 + 2;
    
    echo($variable); //Outputs 3.
    
    ?>
    Code:
    <?php
    
    $onevariable = 1;
    $anothervariable = 2;
    
    echo($onevariable + $anothervariable); //Outputs 3.
    
    ?>
    Cool, whats a function?
    Well, thats easy. A function is something that does something

    For example, echo is something that does something. It takes the parameters you enter in brackets and outputs them.

    e.g echo(PARAMETERS);

    Where parameters will be a string, variable or something else.

    There are a variety of functions which come in handy and do different things. There is pretty much a function for everything you will need to do in php. And if there isn't, write it yourself.

    How do I write one myself?
    Easy.

    do this:
    Code:
    <?php
    
    function multiply($num1,$num2)
    {
    echo($num1 * $num2);
    }
    
    multiply(6,10); // This will output 60 because multiply() multiplies your two parameters.
    
    ?>
    Anyway, thats the end. You've probably got alot of questions because this is probably not detailed or explanatory enough, so go ahead and ask anything. I'll get back to ya ASAP.

    And if you notice anything wrong with this. Tell me, coz I'd probably want to fix it.

    cya

  2. #2
    Resident EE mtekk's Avatar
    Join Date
    Dec 2007
    Location
    Minnesota
    Posts
    469

    Default Re: [PHP] Echoing, Variables and Introduction to Functions

    Also note that you don't have to use echo as a function (in reality it is not a function but rather an object stream).

    e.g. You'll usually see:
    PHP Code:
    <?php echo "Hello World!"?>
    rather than:
    PHP Code:
    <?php echo("Hello World!"); ?>
    They will both output: Hello World!

    echo is similar to a C++ ostream object, any mixture of variables can be output through it.
    PHP Code:
    <?php 
    $numb 
    5;
    $food "pizzas";
    echo 
    "I'm so hungry I could eat " $numb " " $food "!";
    ?>
    The result will be: I'm so hungry I could eat 5 pizzas!

    Yes, there is a C style printf() function as well (at least in PHP5+).

  3. #3
    Fresh Paint
    Join Date
    Jun 2008
    Location
    Adelaide
    Posts
    29

    Default Re: [PHP] Echoing, Variables and Introduction to Functions

    I have got into the habit of using brackets with echo.

  4. #4
    Paradox Sausage DaveW's Avatar
    Join Date
    Nov 2005
    Location
    Scotland, UK (NOT England)
    Posts
    5,550

    Default Re: [PHP] Echoing, Variables and Introduction to Functions

    Code:
    echo "I'm so hungry I could eat " . $numb . " " . $food . "!";
    I hate it when you get a complex piece of code that looks like this. Hi guys, you're really gonna need a proper editor for PHP, because syntax highlighting is frankly a must-have.

    I'd opt for Notepad++ if you like it simple, but one of my personal favourites is PSPad, simply because you get a nice integrated FTP system. That makes a big difference if you're uploading to a live environment.

    -Dave
    Quote Originally Posted by jdbnsn
    Ideas are just knowledge soaked in alcohol.
    Quote Originally Posted by jdbnsn
    Did I just get in a Volvo? Volvo's don't have guns!

  5. #5
    Resident EE mtekk's Avatar
    Join Date
    Dec 2007
    Location
    Minnesota
    Posts
    469

    Default Re: [PHP] Echoing, Variables and Introduction to Functions

    Quote Originally Posted by DaveW View Post
    Code:
    echo "I'm so hungry I could eat " . $numb . " " . $food . "!";
    I hate it when you get a complex piece of code that looks like this. Hi guys, you're really gonna need a proper editor for PHP, because syntax highlighting is frankly a must-have.

    I'd opt for Notepad++ if you like it simple, but one of my personal favourites is PSPad, simply because you get a nice integrated FTP system. That makes a big difference if you're uploading to a live environment.

    -Dave
    That's really not that complex, or ugly. It reads quite easily, using the printf statement, where the variables are not immediately placed in, seems more "ugly". Then again if you don't use nice (reasonably descriptive, e.g. don't use $i, $j, or $k unless it is a iterative variable in a loop) variable names I can see how printf would be less ugly. Syntax highlighting is nice to have, though not entirely necessary. In general the regular MS notepad really sucks for writing code in.

  6. #6
    Paradox Sausage DaveW's Avatar
    Join Date
    Nov 2005
    Location
    Scotland, UK (NOT England)
    Posts
    5,550

    Default Re: [PHP] Echoing, Variables and Introduction to Functions

    That statement isn't ugly. But that's just echoing a simple statement. When you're programming a real application, you're going to have HTML tags and a lot of variables in there. Also, you neglected to mention the "" = '' part of echo, allowing you to echo quotes and such to the screen.

    So while your sample code isn't ugly, it's also not entirely practical. People writing for a practical purpose will very quickly find that their echo statements become difficult to follow without some serious organisation.

    -Dave
    Quote Originally Posted by jdbnsn
    Ideas are just knowledge soaked in alcohol.
    Quote Originally Posted by jdbnsn
    Did I just get in a Volvo? Volvo's don't have guns!

Posting Permissions

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