Why do strings behave like an array in PHP 5.3?

Asked
Active3 hr before
Viewed126 times

7 Answers

array
90%

Meta Stack Overflow ,Stack Overflow en español,Stack Overflow em Português, Stack Overflow Public questions & answers

You can access strings like an array and prior PHP 5.4 offsets like your name were silently casted to 0, means you accessed the first character of that string:

character | p | j | b | a | 0 | 1 |
   -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -
   index | 0 | 1 | 2 | 3 | 4 | 5 |
88%

After 5.3 such offsets will throw a notice, as you can also read in the manual:,As of PHP 5.4 string offsets have to either be integers or integer-like strings, otherwise a warning will be thrown. Previously an offset like "foo" was silently cast to 0.,You can access strings like an array and prior PHP 5.4 offsets like your name were silently casted to 0, means you accessed the first character of that string:,It's working fine in PHP 5.5, but in PHP 5.3 the condition returns true while $host contains a string like pjba01. It returns the first letter of $tierHosts['host'], that is, p.

I have this code:

$tierHosts['host'] = isset($host['name']) ? $host['name'] : $host;
load more v
72%

After 5.3 such offsets will throw a notice, as you can also read in the manual:,As of PHP 5.4 string offsets have to either be integers or integer-like strings, otherwise a warning will be thrown. Previously an offset like "foo" was silently cast to 0.,You can access strings like an array and prior PHP 5.4 offsets like your name were silently casted to 0, means you accessed the first character of that string:,There's a lot been written about PHPExcel and memory use, and I'm not going to repeat it all here.

I have this code:

$tierHosts['host'] = isset($host['name']) ? $host['name'] : $host;
load more v
65%

It's working fine in PHP 5.5, but in PHP 5.3 the condition returns true while $host contains a string like pjba01. It returns the first letter of $tierHosts['host'], that is, p.,What's so wrong with my code?,You can access strings like an array and prior PHP 5.4 offsets like your name were silently casted to 0, means you accessed the first character of that string:,I am trying to convert indexed array to normal array. Basically what get is: And I need is: I tried over Google but not information found. I think this kind of...

I have this code:

$tierHosts['host'] = isset($host['name']) ? $host['name'] : $host;
load more v
75%

PHP arrays can contain int and string keys at the same time as PHP does not distinguish between indexed and associative arrays. , Because the value of an array can be anything, it can also be another array. This enables the creation of recursive and multi-dimensional arrays. ,Example #15 Recursive and multi-dimensional arrays, An array can be created using the array() language construct. It takes any number of comma-separated key => value pairs as arguments.

An array can be created using the array() language construct. It takes any number of comma-separated key => value pairs as arguments.

array(
   key => value,
   key2 => value2,
   key3 => value3,
   ...
)
array(1) {
   [1] =>
   string(1)
   "d"
}
array(4) {
   ["foo"] =>
   string(3)
   "bar" ["bar"] =>
   string(3)
   "foo" [100] =>
   int(-100)[-100] =>
      int(100)
}
array(4) {
   [0] =>
   string(3)
   "foo" [1] =>
   string(3)
   "bar" [2] =>
   string(5)
   "hello" [3] =>
   string(5)
   "world"
}
array(4) {
   [0] =>
   string(1)
   "a" [1] =>
   string(1)
   "b" [6] =>
   string(1)
   "c" [7] =>
   string(1)
   "d"
}
array(7) {
   [1] =>
   string(1)
   "g" [-1] =>
   string(1)
   "d" ["01"] =>
   string(1)
   "e" ["1.5"] =>
   string(1)
   "f" [0] =>
   string(1)
   "h" [""] =>
   string(1)
   "j" [2] =>
   string(1)
   "l"
}
load more v
40%

A string is series of characters, where a character is the same as a byte. This means that PHP only supports a 256-character set, and hence does not offer native Unicode support. See details of the string type. , When a string is specified in double quotes or with heredoc, variables are parsed within it. , The simplest way to specify a string is to enclose it in single quotes (the character '). , When a string is evaluated in a numeric context, the resulting value and type are determined as follows.

My name is "MyName".I am printing some Foo.
Now, I am printing some Bar2.
This should print a capital 'A': A
load more v
22%

Fortunately for PHP developers, there is the PHP Standards Recommendation (PSR), comprised of the following five standards:,View More Freelance Developers,Ruby on Rails Developers,React Native Developers

Not sure how to use foreach loops in PHP? Using references in foreach loops can be useful if you want to operate on each element in the array that you are iterating over. For example:

$arr = array(1, 2, 3, 4);
foreach($arr as & $value) {
   $value = $value * 2;
}
// $arr is now array(2, 4, 6, 8)
load more v

Other "array-undefined" queries related to "Why do strings behave like an array in PHP 5.3?"