function news5($newsarray) {
$str = '';
foreach($newsarray as $value) {
$str.= '<h3>$value['title']</h3>';}
return $str;
}
echo news5($newsarray);
$newsarray is a two - dimensional array, i want to display first value of every subarray in h3 but it doesnt work, without h3 tag everything is fine, but i want it in h3. pls help!
It says "Parse error: syntax error, unexpected 'title' (T_STRING) in C:\xampp\htdocs ews ews.php on line 7"
Try this it will work :
function news5($newsarray) {
$str = '';
foreach($newsarray as $value)
{
$str.= "<h3>".$value['title']."</h3>";
}
return $str;
}
echo news5($newsarray);
Certain characters have special significance in HTML, and should be represented by HTML entities if they are to preserve their meanings. This function returns a string with these conversions made. If you require all input substrings that have associated named entities to be translated, use htmlentities() instead.
You are ending your string with an unescaped single quote. So your string just is
'<h3>$value['
And then the plain title
appears. That is causing the unexpected {string}
. You would either have to escape your quotes
'<h3>$value[\'title\']</h3>'
or use different (double) quotes in one part of your statement - either the array key or the surrounding quotes.
Another problem you will be facing: Variables in single quotes don't evaluate. So your echoed result will be:
$value['title']
which is not what you want (you want the content of that array):
$str .= "<h3>{$value['title']}</h3>";
where the {}
are optional.
use this it might help "<h3>$value['val']</h3>"
$str .= '<h3>';
$str .= $value['title'];
$str .= '</h3>';
$str.= '<h3>$value['title']</h3>';
Strings wrapped in single quotes don't evaluate variable references, as explained in the documentation; you're also not properly opening and closing the single quotes, causing a parse error; double quotes or concatenation would solve that, but you also need to keep in mind that values must be properly escaped to be used in HTML.
$str .= '<h3>' . htmlspecialchars($value['title'], ENT_QUOTES, 'UTF-8') . '</h3>';
See also: htmlspecialchars()