Learning PHP | #1 | Basic Syntax And Using Variables

learning php
PHP is a dynamic, HTML embedded scripting language that is used a lot by millions of websites around the world. To add onto that, it works swiftly with MySQL, which we’ll focus on later.

First off, we actually to know the basics, and the syntax of a PHP document. Open your favourite text editor and save this as a .php file.


<?php

?>

You always have to structure your PHP like that, otherwise the stuff won’t know what your talking about. One of the easiest things to do in PHP is write something, sending it to the web browser.


<?php
echo = "Pixel Clouds";
?>

This is using the echo function, although you could also use print for the same effect.


<?php
print = "Pixel Clouds";
?>

What if we want to add a comment to our new PHP code? Well there are several ways, you can use:

  • CSS comments: /*This is a comment*/
  • Javascript comments: //This is a comment
  • Using the hash key: #This is a comment

In PHP, variables can be very useful, they can be used to temporarily store data. They must always start in a dollar sign, and they may contain letters, numbers and underscores, however the first character cannot be a number.

Let’s make a simple variable containing the user’s IP address.



<?php
$userIP = $_SERVER["REMOTE_ADDR"];
?>

Now we’ve got their IP address, let’s build on it a little bit more using what we’ve just learnt.


<?php

$userIP = $_SERVER["REMOTE_ADDR"];

$message = "That's your IP, wow!";

echo "$userIP";

echo "
$message";

?>

Explained:

  • Creating a variable of $userIP
  • Getting the IP of the user via $_SERVER["REMOTE_ADDR"];
  • Creating another variable of $message
  • Sending the information stored over to the web browser to display via echo “$userIP”; and echo “
    $message”;

If you haven’t already noticed we’ve just created a very simple IP checker. With 4 lines of PHP!

Part 2 coming soon! :)

Thanks,

Arjun Vasudeva
http://avn8.com

Written by Ben Bate

I'm Ben, a freelance designer from Plymouth, England specialising in UI design. I'm the director of iPhone app development company, PixelBit Apps Ltd and also run Pixel Clouds design blog.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>