Sunday, March 29, 2009

PHP: Variable name separator

Share at Facebook

Lets say you have two string variable. And you want to join them. You can easily join them using dot(.) operator. But here is a bit different way which most of the people not use always.

For example you have two variables $str1 = "command" and $str2 = "line", Now you want to print "Commandline" using the two variables. You can easily print them as below

$str1 = "command";
$str2 = "line";
print $str1.$str2;

But here is a bit different way.

$str1 = "command";
$str2 = "line";
print "Input from {$str1}{$str2} arguments\n";

At above example you can see that I have used open and closing second braces{...} at the both end of the variable. This will separate the two variables from each other inside the double quote("")

Here is another way to use second braces. This time the open second braces are just after the dollar($) sign of the variable name. This will do the same as above one.

$str1 = "command";
$str2 = "line";
print "Input from ${str1}${str2} arguments\n";

Not sure whether you know it or not. I used this same thing at Perl. Or you can say it always requires to use this style if you are playing with reference with hash inside array or array inside hash, and more at Perl.

1 comment:

Viable Network said...

$idcount=mysql_query("SELECT * from `tablenameyour` where id = LAST_INSERT_ID(fieldname)");
$idcount1 = mysql_fetch_array();
echo $idcount1;