0

Why this does not work?

  $stringhaha ="     1 => General,
      2 => Business,
      3 => Entertainment,
      4 => Health,
      5 => Politics,
      6 => Sci/Tech,
      7 => Sports,
      8 => News";

$all_categories = array($stringhaha);

print_r($all_categories);

(will give an array with 1 item.)

While this works: If I include the variable content like this it will create properly an array with 8 items:

$all_categories = array(1 => General,
      2 => Business,
      3 => Entertainment,
      4 => Health,
      5 => Politics,
      6 => Sci/Tech,
      7 => Sports,
      8 => News);

print_r($all_categories);
4
  • 5
    This is one of those times when I wish PHP was just a little less forgiving. People get so used to being able to do whatever they want... Commented Aug 17, 2010 at 11:02
  • By the way your second example "works" but you should enclose all those string literals in quotes. Another example of PHP being too forgiving. Commented Aug 17, 2010 at 11:08
  • Thanks for all the comments. How could I make the array function in php accept a variable (That contains proper array input when copied directly into the array function?) This seems to be impossible as per my example as well. Commented Aug 17, 2010 at 11:26
  • 1
    Ok this seems to be impossible. Just found this in php docs array type description: Converting to array For any of the types: integer, float, string, boolean and resource, converting a value to an array results in an array with a single element with index zero and the value of the scalar which was converted. In other words, (array)$scalarValue is exactly the same as array($scalarValue). Commented Aug 17, 2010 at 11:33

3 Answers 3

2

What is happening is exactly what you should expect: you've declared an array that contains one string.

It doesn't matter that your string looks like an array to us humans, PHP is merely PHP, and can't magically detect that you want it to parse an array from a string.

giorgio79, meet PHP Docs, your new best friend.

Sign up to request clarification or add additional context in comments.

1 Comment

Ok thanks. Here is some more info for others coming after me: php.net/manual/en/language.types.array.php
2

It's called language syntax. You cannot do whatever you want. You have to speak the language how it was designed.

This doesn't work either

message = hello

Why? Because it's not syntactically correct. Same applies for your example with array.

This is correct

$message = 'hello';

Every language has rules and you have to respect them. Good luck.

Comments

0

I think the correct syntax is:

$all_categories = array(1 => "General",
    2 => "Business",
    3 => "Entertainment",
    4 => "Health",
    5 => "Politics",
    6 => "Sci/Tech",
    7 => "Sports",
    8 => "News");

print_r($all_categories);

You do want an array of strings, right?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.