I have some little confusion. I want to access the array as follows.
$_POST['un'];
or
$arr['empno'];
But when I try in double quotes, it gives out a compile time error I tried the following:
echo "welcome $_POST['un']";
in un i saved username by query string which is out of this question i think.. .so i write welcome i also tried
echo "$array['emp']";
it also gives me an error. Whats the problem?
Further adding to Fred's comment, if you would like to use array variable, you can use it like:
echo "welcome $_POST[un]";
This will output the value of array variable
When using double quotes in php strings you are telling php to interpret the string before printing it.
When using (associative) arrays or methods, you need to use the {}
brackets around the value to make it work.
Simple variants, indexed arrays, etc. can be parsed by just using the double quotes, but when using associative arrays and methods, you will need the bracers.
More (and more in depth) info here: Php docs (strings)
echo "Some text... $array['key']"; // Bad
echo "Some text... {$array['key']}"; // Good
echo "Calling $var (simple) and {$var} (complex) is basically the same.";
If you want to do the 'cheapest' type, double quotes is not the way to go, rather go with concatenation and single quotes:
echo 'Some text... ' . $array['key'];
Enclose the references to the array with { and }
echo "welcome {$_POST['un']}";
and
echo "{$array['emp']}";
It helps a lot !!!!!