Thursday 14 April 2011

PHP: Declaring and Printing Variables

So i decided to go ahead and learn how to declare and display my variables in PHP. First thing i noticed is when declaring a variable you do not need to specify what type of data your variable is. Such as an integer, string, character, etc. Which i found really weird, but I got use to it. Next thing i noticed was I had a hard time using \n to create a newline in PHP. Which is a command. I just found it was a syntax error(aka parse error in PHP). But anyhow here is some code I created in order to test some things. Feel free to read the English parts of my code for non programmers. It might amuse you.

Source Code
----------------------------------------------------------------------------------------------------------------------------

<html>
   
   <body>Welcome to my PHP Test Page
    <?php /*Declaring and printing variables*/
        $var = 5;
        $name = "Hello World";
        $test = array("Hello","this","is","an","array");
    ?>

    <p>
    Testing out, printing declared variables <br />
   
    printing 5 to the screen from variable <?php print('$var ');?>  <br /><br />
   
   
    <?php print("$var");?> <br /><br /> 
   
    Well that seemed to have worked out nicely, Now wth is the code to print print a newline. <br />
    instead of having to use HTML's "br" to create a new line out of my php scope<br /><br />
   
   
    Time to print a string using echo and print command<br />
   
    <?php echo "$name"; print("   $name   "); ?> <br /><br />
   
    Time to print an array<br />
   
    <?php print_r($test); ?><br />
   
    Well that was really unexpected so much for that command being of any use.<br />
    Maybe i used it wrong oh well time to find another way to print an array.<br />
   
    <?php for($i = 0; $i<5; $i++){print("$test[$i] ");} ?><br /><br />
  
  
    Well that seemed to have worked out a lot nicer. Using a loop and accessing each index of the array was nicer then print_r.<br />
    I think I will end the tests for this blog. Thanks for reading!<br />
   

    </p>

</body>
</html>
 --------------------------------------------------------------------------------------------------------------------------
End of Source Code


 What is actually displayed here. So you can see what exactly I did. 

Welcome to my PHP Test Page
Testing out, printing declared variables
printing 5 to the screen from variable $var

5

Well that seemed to have worked out nicely, Now wth is the code to print print a newline.
instead of having to use HTML's "br" to create a new line out of my php scope

Time to print a string using echo and print command
Hello World Hello World

Time to print an array
Array ( [0] => Hello [1] => this [2] => is [3] => an [4] => array )
Well that was really unexpected so much for that command being of any use.
Maybe i used it wrong oh well time to find another way to print an array.
Hello this is an array

Well that seemed to have worked out a lot nicer. Using a loop and accessing each index of the array was nicer then print_r.
I think I will end the tests for this blog. Thanks for reading!


12 comments:

  1. Wow, you actually made this really easy to follow. Hopefully this will be the start to a great tut!

    Thanks again!!

    ReplyDelete
  2. i like how you make it easy enough that its understandable even if you're not very good at programming

    ReplyDelete
  3. One day I hope this blog will be a full featured php tutorial. thanks! hit me back up

    ReplyDelete
  4. this is a lot less confusing that math :D

    ReplyDelete
  5. you could prolly teach to be honest.
    p.s. nice C cheat sheet thats nifty

    ReplyDelete
  6. you should help me with this stuff :)

    ReplyDelete
  7. I think you found your calling with intstructing. :)

    ReplyDelete
  8. The fact that you gave the code, and then showed what it was supposed to look like seriously helped so much. It's a lot easier to follow along.

    ReplyDelete
  9. friends dont let other friends do php!

    ReplyDelete
  10. do you think maybe one day i'll be able to understand this mr.cheesey?

    ReplyDelete