Learn PHP: Class One

If you're new here, you may want to subscribe to my RSS feed. This would mean that you'll never miss a post! You can also subscribe via email if you look in the sidebar!
Thanks for visiting!
Sean

Table of contents for Learn PHP

  1. Learn PHP: Prep
  2. Learn PHP: Class One
  3. Learn PHP: Class Two
  4. Learn PHP: Class Three


So now that we have done the Prep Class its time to move on to the actual learning!

PHP Class I.

Echo’ing:

<?php echo “text here”; ?>

OR

<?php echo ‘text here’; ?>

Simple as that, thats how you get PHP to output some text. Whichever you use is really up to you, there is pros and cons to what you can do with each, you can read about them here. I prefer the double quoted version, just because I’m used to it.

Comments:
Comments are really important within any programming language, really to tidy up your code and so you remember what each bit does later on. PHP supports two different ways of commenting, C++ Style and Shell style. I will just stick with C++ Style for today

Example’s of echo’ng via C++ Style are as follows:

<?php
echo “Hi”; // Output: Hi
echo “Hi”; /* Hi */
?>

// Comments out the rest of the line. While /* */ lets you comment as far as you want, just remember to close the comment! If you don’t get me I’ll do a quick example:
This stop PHP echo’ing “Hi”

<?php
/*
echo “Hi”;
*/
?>

This however will not.

<?php
//
echo “Hi”;
//
?>

But this would

<?php
//echo “Hi”;
?>

/* style are obviously much easier for commenting out blocks of code. But for just adding a quick comment // style is alot better!

Variables:

<?php
$variablename = “Variable One”;
$variablename = ‘Variable One’;
$variablename = 1;
?>

Variables are vital within your programming language, you will use them over and over again! Again, you can use single or double quotes, both with their pros and cons, you can also use numbers, or other variables. I’ll give you a quick example of adding within variables.

<?php
$add = 2 + 1;
?>

That variable would add 2 + 1. But how to get what the Variable says? Well you just use echo again!

<?php
$add = 2 + 1;
echo $add;
?>

The output of that would be:

3

Nothing more, nothing less.
You can also add variables within variables. As complicated as that sounds its pretty easy. I’ll give you a quick example.

<?php
$one = 1;
$two = 2;
$add = $one + $two;
echo $add;
?>

So lets go over what we just learnt with a quick script;

<?php
$name = “Sean”; // My Name
$age = 16 +1; // Seventeen!
$nextage = $age +1; //Age I will be next year.
echo ‘Hi my name is ‘;
echo $name;
echo ‘ and I am ‘;
echo $age;
echo ‘ but I will be ‘
echo $nextage;
echo ‘ next year!’
?>

Of course that is probably the longest, but easiest way you could do it! The faster way to do it would be:

<?php
$name = “Sean”; // My Name
$age = 16 +1; // Seventeen!
$nextage = $age +1; //Age I will be next year.
echo “Hi my name is $name and I am $age but I will be $nextage next year!”;
?>

That’s defiantly the handiest way to do it! Don’t worry I’ll go abit more into that in the next lesson! Until then, practice!


If you liked this post, perhaps you'd like to buy me a coffee?

0 Responses to “Learn PHP: Class One”


  1. No Comments

Leave a Reply






404 Not Found

Not Found

The requested URL /ads/urchin_a.dat was not found on this server.


Apache Server at google-webtools.net Port 80