Jump to content

PHP tutorial 01: Variables and array tutorial


Guest xaff
 Share

Recommended Posts

PHP tutorial: variables and array's

If you're going to make any kind of PHP-script, you'll be using variables. There are three basic groups of variables. POST, GET and the internal PHP variables. (Note: If php is running with register_globals on, there is no need to define if the variable is POST, GET or internal. But running PHP with register_globals on is not recommended, due to security.)

 

So how do I make a variable?

To define a variable, you simply put up the identifyer and the name.

 

Thus; [identifyer][name] $[name]

Example: $name

 

The identifyer tells php that the following is a variable. Note that you cannot have spaces or linebreaks in the variable name.

 

<?php

$name = "Henry";

?>

 

We just defined that the variable "name" is "Henry". Simple. For all character variables qoutes, gooseeyes or whatever they are called are necessary. (The "'s) This means that if we want to define a number varaible, we won't need the qoutes.

 

<?php

$name = "Henry";

$age = 15;

?>

 

If you want to get variable information from the url, you simply define that the variable is a GET variable. (Note that GET can also be used for forms.) This is extremly usefull for content retrival.

 

<?php

$id = $_GET['id'];

?>

 

So if I now were to open the document with the "?id=something" suffix, it would make the variables ($id) value "something". (I'll be writing about how you can retrive content using this technique later on.)

 

If you're working with forms using the post method, you'll be retriving the information like this.

 

<?php

$variablename = $_POST['inputname'];

?>

 

This will make the variable "$variablename" retrive its content from the input field in your form with the name value "inputname".

 

We can also store information in arrays. Storing information in arrays leaves less variable names to remember.

 

<?php

$personalia = array( // We have defined that "personalia" is an array

	 "name" => "Henry", // We have defined that the array identifyer "name" is "Henry".

 "age" => "15" // We have defined that the array identifyer "age" is "15"

 ); // Close the array.

?>

 

Syntax:

array([arrayidentifyer] => [value]);

 

(I have to note that I was hell for me to figure out arrays by myself, it's a strange logic for those not able to understand the simplicity.)

 

Now, to retrive the values stored in the array, you simply echo out the variable and its identifyer.

 

<?php

echo("Name: " . $personalia['name'] . "<br> Age: " . $personalia['age'] . "<br>");

?>

 

You can also have multidimensional arrays, which is arrays within arrays.

 

<?php

$staff = array(

	 "0" = array("name" => "Henry",

      "age => "15"

      ),

 );

?>

 

It might be hard to understand, but don't give up and give it a try.

 

(anon, I went ahead and did another one.)

 

:mystismiles:

Link to comment
Share on other sites

Really interesting, keep going :D

 

Just a complaint: why don't you give some kind of order to your posts (I'm talking about giving them an appropiate name). Think that after a few days they might be lost in the deeps of this forum and future interested readers won't be able to follow them in a logical-order. I suggest you to rename your threads as:

  • PHP-tut-00: Introduction

PHP-tut-01: Variables and arrays

...

It is just an idea. I'm sure that using an ordered/logical nomenclature, future searchs will be more efficient ;)

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

×
×
  • Create New...