Php newbie questions!

Hey folks,
i m learning php as i rolling along i m coming out with question so here r some.

  1. In maths why in addiction its $var += 4, where $var can be any number
  2. what is difference between variable and arrays?
  3. here is my code. i m getting a undefined index at last echo. also where i m printing 2 array. the second array isn’t being printed instead its saying o. why?
tml>
    <head>
        <title>Arrays</title>
    </head>
        <body>
            <h1>Arrays</h1>
            <?php 
            $numbers = array(5,10,15,20);
            echo $numbers[1];
            echo "<br/>";
            ?>
            <?php
            $number = array(5,10,"Hello", array("world",15));
            [B]echo $number[2][1];[/B]
            $names = array("first_name" => "Ammar");
            [B]echo $names["frist_name"];[/B]
            ?>
        </body>
</html>

Alright :slight_smile:

because $var += 4 can by typed faster, and is more readable, than $var = $var + 4;
The same way $var++ is the same as $var = $var + 1;
They are just shortcuts because you use increments quite a lot.

An array is also a variable, but it’s a variable that holds other variables.
They are used because you sometimes have a lot of variables and you like to store them in a good compact way, and you can iterate them (take a look at the for “function” (it’s technically speaking not a function, but rather a language construct).

Consider


$person1='Anton';
$person2='Bert';
$person3='Cornelis';

How would I put all those variables on the screen. I would not like to do it like this:


$person1='Anton';
$person2='Bert';
$person3='Cornelis';
echo $person1;
echo $person2;
echo $person3;

Because then every time I need to add a person I also need to add an “echo” statement.

Instead, what I can do is:


$persons = array('Anton', 'Bert', 'Cornelis');
foreach($persons as $person)
{
   echo $person;
}

Isn’t that handy? :slight_smile:


$names = array("first_name" => "Ammar");
[B]echo $names["frist_name"];[/B]

Please take a good look at how you wrote first_name in these 2 lines.
Hint: the second one has a typo :wink:

thanks XTX.dumb me to have a typo, though one question left which i asked in 3rd one is that i can’t echo my second array. like its echoing e . here is the code

            
<?php
$number = array(5,10,"Hello", array("world",15));
echo $number[2][1];
 ?>

where as it should echo world in 2nd array

why do you think it has key ‘2’?
and what output you expect when you echoing an array?

arrays of course.

well, count it’s key more careful

** SPOILER ALERT **

[SIZE=“1”]It’s $number[3][0]

And your script is now echoing “e” because the character “e” has index 1 in the word Hello
0: H
1: e
2: l
3: l
4: o
[/SIZE]

so how do i echo whats in next array?


<?php
$num = array(1,2,3 array(4,"Hello",5);
echo $num[1][5];
?>

where 5 should echo Hello? where am i going wrong

Do you mean with a comma after the 3, and a closing parenthesis at the end?


$num = array(1,2,3, array(4,"Hello",5));

In that case, the array will be $num[3], so the Hello will be $num[3][1]

i read the tutorial i was studying what he wanted to say if i wanna echo array inside of a array. like

<?php
$num = array(1,2,3 array (5,6,7));
echo $num[3][2];
?>

that will echo the nested array value which is 7.anyhow i got it right, moving along i was studying implode and explode. and i din’t get why they r used?

Have a read of implode and [URL=“http://www.php.net/manual/en/function.explode.php”]explode for examples of how they’re used.

Arrays are indexed starting from 0, not 1. So to get the ‘b’ in array(‘a’, ‘b’, ‘c’) you would do $var[1];

hey folks, as i m moving towards my php learning joruney i m facing another probelm. i got the idea of concatination but i m unable to follow inplace subsitution (the curly braces replacement for .) here is a query i m following from a tutorial but something is wrong after WHERE. can anyone help me. its giving me error

$result= mysql_query("SELECT * FROM pages WHERE  subject_id = {$row["id"]}");

Edit: i posted this Question in MySql forums and i was told its a php question and the guy told me there to replace double quotes inside string with single quote. whch i did but still getting an error

Why don’t you post here corrected code, with single quotes?
Did you really replace it?
And what error message you get? You may be don’t know, but we have not telepathy here yet.

lol @ Telepathy. here is the code

<?php 
    $result= mysql_query("SELECT * FROM subjects");
    if(!$result){
        die  ("<strong>". mysql_error() ."</strong>");
    }
    while ($row = mysql_fetch_array($result)){
    echo "<li>".$row["menu_name"] ."</li>";
        //echo pages related to subjects
            $result= mysql_query("SELECT * FROM pages WHERE subject_id = {$row['id']}");
            if(!$result){
            die  ("<strong>". mysql_error() ."</strong>");
        }
        echo "<ul class=\\"pages\\">";
        while ($row = mysql_fetch_array($result)){
        echo "<li>".$row["menu_name"] ."</li>";
        }
        echo "</ul>";
}
?>

I am sorry.
But I didn’t said “post your code here”. I said “what error message you get?”

  1. For better understanding and reliability, separate assigning string to a variable from function call. e.g.
$sql = "SELECT * FROM subjects";
$result = mysql_query($sql);
  1. try this single query:
SELECT * FROM subjects s, pages p WHERE subject_id = s.id

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ‘’ at line 1

The first loops fine. but the 2nd statment where it says where have some problem

wht will this do

SELECT * FROM subjects s, pages p WHERE subject_id = s.id

the s,p and s?

I can’t see ‘’ character in your second qouery.
You’re probably running another one.

To add $sql variable to the error message output would be mighty helpful.

also, you mix your variables, and your queries must be joined to single one

the s,p and s?

aliases for the table names