Why can't I access array element in string here? [duplicate] - php

This question already has answers here:
PHP parse/syntax errors; and how to solve them
(20 answers)
Closed 6 years ago.
I have an array
$array = ['first'=>'hi','second'=>'bye'];
Why following syntax is not working
echo " i wanna print $array['first']";
The error message is
Parse error: syntax error, unexpected '' (T_ENCAPSED_AND_WHITESPACE),
expecting identifier (T_STRING) or variable (T_VARIABLE) or number
(T_NUM_STRING) in
But when I tried
echo "i wanna print $array[first]";
did work fine.
So can someone explain what difference single quotes (') making here. And what does above error really mean, any ideas?

here php will not able to parse the multi dimension array from double quote string, to acheive same functionality you have to enquote the array variable inside {} brackets.
Try below line of code it will work without error.
echo " i wanna print {$array['first']}";
I hope this help you in understanding.

Use:
echo "i wanna print ".$array['first'];
Instead of
echo " i wanna print $array['first']";

Related

in php how can I access a index of an array which is stored in a variable as string [duplicate]

This question already has answers here:
PHP parse/syntax errors; and how to solve them
(20 answers)
Closed 4 years ago.
$row['test']='this is test index';
$test="$row['test']";
echo $test;
in the above code in variable test I want to store name of the varible $row['test'] and then try to print test. The desired output should be 'this is test index' but the output I am getting is
Parse error: syntax error, unexpected '' (T_ENCAPSED_AND_WHITESPACE), expecting '-' or identifier (T_STRING) or variable (T_VARIABLE) or number (T_NUM_STRING) . So my question is how can I actually get the value of variable $row['test'] stored in the variable test as string.
Just Remove the double quotations from "$row['test']"
This is works
<?php
$row['test']='this is test index';
$test=$row['test'];
echo $test;
?>

Parse error: syntax error, unexpected 'connected' (T_STRING) in C:\xampp\htdocs\koneksi\update.php on line 5 [duplicate]

This question already has answers here:
PHP parse/syntax errors; and how to solve them
(20 answers)
Closed 4 years ago.
I got Error like this [Parse error: syntax error, unexpected 'connected' (T_STRING) in C:\xampp\htdocs\koneksi\update.php on line 5]
<?php
if($_SERVER['REQUEST_METHODE']=='POST){
include('connected.php');
$room=$_POST['NoRoom'];
$status=$_POST['RoomStatus'];
$Sql_Query="UPDATE cfg_init_room SET number='$room, status_code='$status';
if (mysqli_query($con,$SQl_Query));
{
echo 'Status Updated';
}else{
echo'Gagal Update Status';
}
}mysqli_close($con);
?>
On this line if($_SERVER['REQUEST_METHODE']=='POST){ there is a missing ' after POST.
Make that line if($_SERVER['REQUEST_METHODE']=='POST'){ and it should work.
On most IDEs you will see a shift in text color between strings and functions, just like here on SO.
That is a telling sign you forgot to close a string.
Edit: now that I look at your code more closely it seems you have another string problem.
At the end in the if else your strings are correct. They shouldn't.
So that means there is another problem with your strings, probably in $sql_query line.

how use strings with $ (not var) in PHP [duplicate]

This question already has answers here:
PHP parse/syntax errors; and how to solve them
(20 answers)
Closed 5 years ago.
I place to the php line a large piece of html code with javascript inserts (with jQuery) in which $ symbols is present.
<?php
$site = <<<SITE_CODE
setTimeout(function(){$(g_utils._f().menu.current_id).trigger('click')}, g_utils.effects.animation(g_utils._f().animation.events.loading, 'delay2'));
SITE_CODE;
echo $site;
?>
PHP takes this as a variable and produces an error.
Parse error: syntax error, unexpected '(', expecting T_VARIABLE or '$'
in D:\site\index.php on line 328
Tell me how to get rid of this problem?
If you single quote the EOL string it will not evaluate any variable:
$site =
<<<'SITE_CODE'
setTimeout(function(){$(g_utils._f().menu.current_id).trigger('click')}, g_utils.effects.animation(g_utils._f().animation.events.loading, 'delay2'));
SITE_CODE;

PHP echo and print Parse Error [duplicate]

This question already has answers here:
PHP parse/syntax errors; and how to solve them
(20 answers)
Closed 7 years ago.
MY code is quite simple.
<?php
echo "hello world";
?>
and i get this message
Parse error: syntax error, unexpected 'world' (T_STRING) in /usr/local/zend/apache2/htdocs/index2.php on line 2
I just copied the code from above on this page because it has the straight bar quotes instead of the curly quotes. I pasted this back into text editor... and wala! it work! thanks guys for the help!!! whoever said the idea about the quotes, i am one upping you or however this site works.

Parse error in PHP file when echoing javascript [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Reference - What does this error mean in PHP?
I keep on getting a "Parse error: syntax error, unexpected '(', expecting T_VARIABLE or '$' in..."
Here's the line thats causing the problem, not sure whats going on here?
$text = "<script>window.setInterval(function(){$('#liveData').load('liveUpdate.php');}, 1000);</script>";
Thanks in advance
You should escape $ in your $text
$text = "<script>window.setInterval(function(){\$('#liveData').load('liveUpdate.php');}, 1000);</script>";
Or you just use single quote.
Because $ in double quote will be followed by a php variable name, $(XXXX is an invalid PHP variable.
So the best way is to separate javascript (jquery) into a .js file.
Try this,
$text = '<script>window.setInterval(function(){$("#liveData").load("liveUpdate.php");}, 1000);</script>';
$text = '<script>window.setInterval(function(){$("#liveData").load("liveUpdate.php");}, 1000);</script>';

Categories