Log in

View Full Version : pretty please (A) +REP



myke
23-08-2008, 11:09 AM
Hey guys,


This will be my third try at learning PHP.

This page stumps me a bit:
http://w3schools.com/php/php_arrays.asp

Could you please explain it in terms that are understandable? :S

Thanks :)

Calon
23-08-2008, 11:21 AM
$names = array("Peter","Quagmire","Joe");


This sorts Peter, Quagmire and Joe into variable/arrays.

so $names[0] has the value of "Peter"
then.. $names[1] has the value of "Quagmire"
then.. $names[2] has the value of "Joe"

myke
23-08-2008, 11:30 AM
Ah, ok, wut about assertive or w.e?

Also, this http://w3schools.com/php/php_looping.asp confuses me a tiny bit, don't they do the same??

Calon
23-08-2008, 11:37 AM
Ah, ok, wut about assertive or w.e?

Also, this http://w3schools.com/php/php_looping.asp confuses me a tiny bit, don't they do the same??
Looping is "while()" so you'd use that to loop code, such as getting things from a database.



// other page code, including connecting to your database.
$query = mysql_query("SELECT * FROM `table`");

while($row = mysql_fetch_array($query)) {
echo "This ". $row["name"] ." That.. ";
}


So "name" is a row within your database, and using while will list every row in the table.

myke
23-08-2008, 06:14 PM
Ok, that sorta confused me :S

This is going to be a long time...

Calon
23-08-2008, 09:38 PM
while() loops code, but you don't really need to loop code often.

Decode
23-08-2008, 09:47 PM
Ok, that sorta confused me :S

This is going to be a long time...
An eaysier to understand exapmle



while ( $value < 10 ) {
echo $value; //echos numbers
$value++; //adds 1 to the number
}


That would display

123456789

Protege
23-08-2008, 09:47 PM
Ok, that sorta confused me :S

This is going to be a long time...

Arrays, sexual - I'll explain them how I get them

Think of an array as a single table of data, say a few names like the first post was

Peter
Quagmire
Joe

Put them into a array, separated by commas

$names = array( 'Peter', 'Quagmire', 'Joe' );By default, unless set, to access 'Peter' would be accessible via


$names[ 0 ];
Or instead of that, you could set them yourself


$names[ 514 ] = 'Peter';
$names[ 415 ] = 'Quagmire';
$names[ 666 ] = 'Joe';
You could access 'Peter' via
$names[ 514 ];You don't have to use numbers as ID keys, for example, you could have the name 'Peter' as a key and have a value behind it

EG:


$names = array( 'Peter' => 'Fat' );or


$names[ 'Peter' ] = 'Fat';Multidimensional Arrays are fun too

Say, you have different surnames, and behind each surname you got people


$surnames = array(
'Smith' => array( 'Mark', 'John', 'Sally' ),
'Barker' => array( 'Joe', 'Dylan', 'Josh' )
);Look, you have two surnames in the array 'Smith' and 'Barker', behind each are the names of the people with that surname.

Lets access 'Smith'


$surnames[ 'Smith' ][ 0 ];Now, that will return 'Mark', for John

$surnames[ 'Smith' ][ 1 ];Same with 'Barker'


$surnames[ 'Barker' ][ 2 ];Will return 'Josh'

Now, you wanted to see how the foreach worked, for example

Just read it as I put it


$array = array( 'Kill', 'People', 'Who', 'Do', 'Not', 'Understand', 'This' );
foreach( $array as $value )
{
echo( $value . '<br />' );
}Say, for each $value in $array

So its going to get each value from $array and echo it

the result will be


Kill
People
Who
Do
Not
Understand
This

Calon
23-08-2008, 10:13 PM
Arrays, sexual - I'll explain them how I get them

Think of an array as a single table of data, say a few names like the first post was

Peter
Quagmire
Joe

Put them into a array, separated by commas

$names = array( 'Peter', 'Quagmire', 'Joe' );By default, unless set, to access 'Peter' would be accessible via


$names[ 0 ];
Or instead of that, you could set them yourself


$names[ 514 ] = 'Peter';
$names[ 415 ] = 'Quagmire';
$names[ 666 ] = 'Joe';
You could access 'Peter' via
$names[ 514 ];You don't have to use numbers as ID keys, for example, you could have the name 'Peter' as a key and have a value behind it

EG:


$names = array( 'Peter' => 'Fat' );or


$names[ 'Peter' ] = 'Fat';Multidimensional Arrays are fun too

Say, you have different surnames, and behind each surname you got people


$surnames = array(
'Smith' => array( 'Mark', 'John', 'Sally' ),
'Barker' => array( 'Joe', 'Dylan', 'Josh' )
);Look, you have two surnames in the array 'Smith' and 'Barker', behind each are the names of the people with that surname.

Lets access 'Smith'


$surnames[ 'Smith' ][ 0 ];Now, that will return 'Mark', for John

$surnames[ 'Smith' ][ 1 ];Same with 'Barker'


$surnames[ 'Barker' ][ 2 ];Will return 'Josh'

Now, you wanted to see how the foreach worked, for example

Just read it as I put it


$array = array( 'Kill', 'People', 'Who', 'Do', 'Not', 'Understand', 'This' );
foreach( $array as $value )
{
echo( $value . '<br />' );
}Say, for each $value in $array

So its going to get each value from $array and echo it

the result will be
Post stuck :) (Forum Moderator) - keep up the unsexual work

Excellent1
23-08-2008, 10:24 PM
Arrays are handy but you don't always need them.
You could use something as simple as:


$name1 = "Bob";
$name2 = "Marley";
echo (' $name1 ');
But stick to arrays as it will be the most professional way of doing things.

Protege
23-08-2008, 10:39 PM
Arrays are handy but you don't always need them.
You could use something as simple as:


$name1 = "Bob";
$name2 = "Marley";
echo (' $name1 ');But stick to arrays as it will be the most professional way of doing things.

First of all that wouldnt work as you'd have to use double quotes

echo( '$name' ); // "$name"
echo( $name ); // "Bob"
echo( "$name" ); // "Bob"

Excellent1
23-08-2008, 10:41 PM
First of all that wouldnt work as you'd have to use double quotes

echo( '$name' ); // "$name"
echo( $name ); // "Bob"
echo( "$name" ); // "Bob"
My bad, waiting on Hypertext to come in talking about parenthesis now :rolleyes:

Calon
23-08-2008, 11:23 PM
Arrays are handy but you don't always need them.
You could use something as simple as:


$name1 = "Bob";
$name2 = "Marley";
echo (' $name1 ');But stick to arrays as it will be the most professional way of doing things.
Why are you using quotes to echo a variable?!



echo (' $name1 ');
The way..


echo $name1;
// OR IF YOUR FANCY
echo( $name1 );

Excellent1
23-08-2008, 11:55 PM
Why are you using quotes to echo a variable?!



echo (' $name1 ');
The way..


echo $name1;
// OR IF YOUR FANCY
echo( $name1 );

Personal preference of my coding :)

Invent
24-08-2008, 12:48 AM
Personal preference of my coding

Using parenthesis' with language constructs doesn't bother me, but putting variables in quotes does :P

Protege
24-08-2008, 12:51 AM
yeah, Its really confusing once you start going "$thisvar is like $great2thisvar and $that"

That confuses the **** into me lols

$thisvar . ' is like ' . $great2thisvar . ' and ' . $that

HyperText: 'TEH FIRSTZ RULEZ OF CODIGNZZ IS NOT TO PUT PARENTTHISTISZ ON A NON-FUNCTZIONZ OKIA??? ITS BAIDZ AND IT MIAKIS ME MADDD'

Want to hide these adverts? Register an account for free!