0

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

3
Contributors
6
Replies
36
Views
5 Months
Discussion Span
Last Post by JamesCherrill
0

Guys,

I'm referring to this tutorial:
https://www.tutorialspoint.com/php/php_arrays.htm

It shows 2 methods of creating an array:

<html>
   <body>

      <?php
         /* First method to create array. */
         $numbers = array( 1, 2, 3, 4, 5);

         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>

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
//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>

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);

Edited by UI

2

You need to go back and review the use of quote marks when defining Strings. Your strings are missing their quote marks. That's what those Notices are telling you - PHP has fixed some of your code by adding the missing quotes.

2

As James. Also:

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

Any values without quotes or $ (or some other tokens) - are considered constants. They are not defined anywhere as such, hence the errors.

0

Guys,

Thanks for your replies but how come everyone's forgotten to answer 2 of my questions in my op ?

0

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:
https://www.tutorialspoint.com/php/php_arrays.htm

Edited by UI

2

php knows that 123 is a number. It can't be the name of a constant, or anything else beause the definition of the language does not allow you to use a number in any of those ways. But abc could be a name of a constant, or anything else, so you need the quotes to show that it's a literal string, not a name.

Edited by JamesCherrill

Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.