Jump to content


Photo

PHP Coding Tutorial 2


  • Please log in to reply
5 replies to this topic

#1 Elric

Elric

    Designer

  • Hosted
  • 2,857 posts
  • Projects:Middle-Earth Expanded
  •  Coder
  • Donated

Posted 09 April 2012 - 08:13 PM

In our 2nd tutorial on PHP we will learn about variables. Variables are used sort of like references. So, if I wanted PHP to echo out Apollo twice instead of typing in:

<?php

echo “I am Apollo”; '<br>'
echo “I am Apollo”;

?>

All I would have to type in is the variable. It can make writing php a whole lot neater and let you repeat certain words without having to type it ALL in again or copy and paste it. So, to begin we will start with a name variable.

<?php

$name = “Apollo”;

echo “I am $name”; '<br>'
echo “I am $name”;
?>

Now save it as “Tutorial 2 – Variables.php” and save it to “(drive letter where you installed xampp):\xampp\htdocs\tutorials”. Make sure xampp is open and apache and MySQL are running. Go to your web browser and type: localhost:2375/tutorials or localhost:80/tutorials depending on which you have and press enter.

Click “Tutorial 2 – Variables.php” and you see… It should project “I am Apollo” two times.

So, you see instead of typing “I am Apollo” as the echo I typed “I am $name” now if I wanted to say: “I am Apollo and I am (blank) years old” then I would have to create a new variable called a number variable. To do a number variable all you have to do is:

<?php
$name = “Apollo”;
$age = 20;
echo “I am $name and I am $age years old”;
?>

See the difference? The difference is that the name variable has “” around the variable. But the number variable doesn’t.. If you were to type:

$age = two;

It would not work because it is a NUMBER but it isn’t the actual symbol type number (1, 2, 3, 4, 5, etc..)

There are other types of variables that you can use to called POST and GET but we will get into thoughs later because that includes html coding to..

That is the end of this tutorial. If you would like to go more in depth with variables sooner rather than later, P.M me. The next tutorial should be out in a few days.

Edited by {IP}Apollo, 09 April 2012 - 08:16 PM.


#2 Hostile

Hostile

    Benefitting Humanity Simply by Showing Up!

  • Veterans
  • 9,551 posts
  • Location:Washington DC
  •  T3A Founder
  • Division:Revora
  • Job:Global Administrator
  • Donated
  • Association

Posted 10 April 2012 - 12:43 PM

Variables are fun you can use them in silly things like page titles.
<?php
$name = "Apollo";
$age = 20;
?>
<title>Yoursite - <?php echo $name; ?> and <?php echo $age; ?></title>

Just remember to declare the variable before using it, the server has to know what it's supposed to present. Try to make sure you don't use echo to create html markup, isolate the html code from the php code for proper markup.

#3 Elric

Elric

    Designer

  • Hosted
  • 2,857 posts
  • Projects:Middle-Earth Expanded
  •  Coder
  • Donated

Posted 10 April 2012 - 02:35 PM

Hostile i appreciate your advice. But, this is the way I learned. I find it looks a whole lot neater and easier to read. I like your style though on some points but this is only PHP not HTML the only HTML thing we will use until we get to the POST and GET functions is the <br> and that's easy enough. I usually just use comments to organize my php instead of:

<?php

$ok = "ok";

?>

<?php

echo "$ok";

?>


MY way of doing it is like this:

<?php

//Variables.

$ok = "ok";

//Here is where the website projects stuff on the actual pages.

echo "$ok";

?>


Edited by {IP}Apollo, 10 April 2012 - 02:36 PM.


#4 Hostile

Hostile

    Benefitting Humanity Simply by Showing Up!

  • Veterans
  • 9,551 posts
  • Location:Washington DC
  •  T3A Founder
  • Division:Revora
  • Job:Global Administrator
  • Donated
  • Association

Posted 10 April 2012 - 05:57 PM

<?php echo "good luck/./" ?>


#5 Elric

Elric

    Designer

  • Hosted
  • 2,857 posts
  • Projects:Middle-Earth Expanded
  •  Coder
  • Donated

Posted 11 April 2012 - 04:09 PM

thanks

#6 {IP}Gil-Galad

{IP}Gil-Galad

    ClickerMonkey

  • Hosted
  • 1,122 posts
  • Location:Pennsylvannia, USA
  • Projects:Too many to name.
  •  T3A Team Chamber Member

Posted 16 February 2013 - 08:41 PM

<?='I like this notation, and using single quotes is quicker except you can't embed variables.'?>




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users