Numerically Indexed Arrays

Fellow Php Buds,

Do you mind telling me, according to you:

  1. How many variables are listed below ? And,
  2. How many numerically Indexed Arrays ?

$employee_array[0] = “Bob”;
$employee_array[1] = “Sally”;
$employee_array[2] = “Charlie”;
$employee_array[3] = “Clare”;

Thank You

  1. 4
  2. 1
1 Like

Guys,

I’m referring to this tutorial:

It shows 2 methods of creating an array:

[php]

<?php      
/* First method to create array. */
$numbers = array( one, two, three, four, five);
         
foreach( $numbers as $value ) {
    echo "Value is $value <br />";
    }
         
    /* Second method to create array. */
    $numbers[0] = "one";
    $numbers[1] = "two";
    $numbers[2] = "three";
    $numbers[3] = "four";
    $numbers[4] = "five";
         
foreach( $numbers as $value ) {
    echo "Value is $value <br />";
    }
?>
      
   </body>
</html>

[/php]

On the first method, I just switched the numerical numbers to numbers in words but I get error! It should’ve worked no matter what the values of the arrays are on (note the first method below).
I changed it to:

[php]

<?php
//2 examples On How To Create Numeric Arrays:
?>
<html>
   <body>
   
<?php      
/* First method to create array. */
$numbers = array( one, two, three, four, five);
         
foreach( $numbers as $value ) {
    echo "Value is $value <br />";
    }
         
    /* Second method to create array. */
    $numbers[0] = "one";
    $numbers[1] = "two";
    $numbers[2] = "three";
    $numbers[3] = "four";
    $numbers[4] = "five";
         
foreach( $numbers as $value ) {
    echo "Value is $value <br />";
    }
?>
      
   </body>
</html>

[/php]

This is the result I get:

**Notice: Use of undefined constant one - assumed 'one' in C:\xampp\htdocs\test\test.php on line 41**

**Notice: Use of undefined constant two - assumed 'two' in C:\xampp\htdocs\test\test.php on line 41**

**Notice: Use of undefined constant three - assumed 'three' in C:\xampp\htdocs\test\test.php on line 41**

**Notice: Use of undefined constant four - assumed 'four' in C:\xampp\htdocs\test\test.php on line 41**

**Notice: Use of undefined constant five - assumed 'five' in C:\xampp\htdocs\test\test.php on line 41**
**Value is one **
**Value is two **
**Value is three **
**Value is four **
**Value is five **
**Value is one **
**Value is two **
**Value is three **
**Value is four **
**Value is five**

My point:

If following works without any errors then the further following too should work:

    /* First method to create array. Example, according to tutorial. This sows no errors. */
             $numbers = array( 1, 2, 3, 4, 5);





    /* First method to create array. Example, according to my editing. This shows errors. */
             $numbers = array( one, two, three, four, five);

The number words in the brackets are strings. They need to be enclosed in quotes:

$numbers = array('one', 'two', 'three', 'four', 'five'); 
5 Likes

I have wondered before about whether it might be better for PHP to throw a syntax error instead of an “assuming undefined CONSTANT” notice.
But such it is.

2 Likes

Well generally speaking, PHP will assume anything not in quotes a constant if no $ sign is appended to the string/ word. It should have tighter errors, but I guess it is what it is.

3 Likes

Wrong! I thought likewise too and another programmer like you in another forum.
We 3 thought the same. But we are wrong!
Let’s see what the others here say.

So far, only one programmer has got it correct out of 6 forums.
2 got it wrong in 2 forums (including this forum).
And other programmers have forgotten to reply to this question both in this forum and other forums. I am reminding everyone. This answer will reveal who really is a pro programmer. No offense to those who got it wrong or get it wrong. Even I, got it wrong. Hence, the question to see if others get it wrong like me and to explain to us why we got it wrong. Those who get it wrong, don’t be offended. We will learn from our mistakes at the hands of those who know better.
Had I not opened this thread, you never would have known you are understanding some important aspect of php wrong. Thanks to this thread, your mistake would now be corrected.

Cheers!

Thanks Guys,

So, many programmers have told me that the worded numbers are strings and I should single quote them.

Wrong way:
$numbers = array( one, two, three, four, five);

Right way:
$numbers = array( ‘one’, ‘two’, ‘three’, ‘four’, ‘five’);

But how come the following is valid then without the single quotes:

$numbers = array( 1, 2, 3, 4, 5);

Why need quotes on one and not the other ?

I was re-learning from:

These are numeric values, they are not strings.
If you put them in quotes, then they would be strings.

3 Likes

One array - one variable listed 4 times.

3 Likes

That’s because there are different types of data. You could have a regular string or numeric string. When you apply double quotes or single quotes, that value becomes a string. When you don’t apply double quotes or single quotes to that value, that value becomes something else. If it’s an actual numeric value, PHP won’t complain about it because all numeric values are considered integer and PHP allows you to not append a $ to that value just like any other programming language. If you did the same thing in say C#, it’d get the same results using C# syntax. If that value is an actual string, PHP will assume that value is referencing a constant.

So

$array = array(0, 1, 2, 3, 4);

Is perfectly fine since it’s all numeric values. However, if you did this

$array = array(0, 1, 2, three, '4');

PHP will assume that three is a constant since it literally has a string value. So PHP would think you are referencing a constant and if that constant doesn’t exist, PHP will complain about it.

Notice that the 4 numeric value is in single quotes. That 4 becomes a string. So testing things like is_int against it will return false. Only is_numeric will return true.

2 Likes

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.