Like in C, can I use a string as an array?
For example:
$a = "abcd";
for ($b = 0; $b <= 3; $b++) {
echo $a[$b];
}
Is a string in PHP an array, or based on arrays as in C?
Like in C, can I use a string as an array?
For example:
$a = "abcd";
for ($b = 0; $b <= 3; $b++) {
echo $a[$b];
}
Is a string in PHP an array, or based on arrays as in C?
Actually yes, but you have to use another syntax:
$a = "abcd";
for ($b = 0; $b <= 3; $b++) {
echo $a{$b};
}
You can go through the whole string by checking on the string length, and getting each letter by using the substr() function:
$a = "abcd";
for($b = 0; $b <= strlen($a); $b++){
echo substr($a, $b, 1).'<br>';
}
Hope this helps!
<?php
$str = "Lorem ipsum";
if (is_array($str)) {
echo "$str is array";
}
else {
echo "$str is not array";
}
?>
Result:
Lorem ipsum is not array
so....
You should use str_split($string)
in order to convert a string to an array
For instance:
var_dump(str_split("abc"));
will translate into
array(3) {
[0]=> string(1) "a"
[1]=> string(1) "b"
[2]=> string(1) "c"
}