Mad_T;11003927 wrote:Is there a neater way to write my first snippet of code ?
yes, but mostly it involves simply fixing all your typos. Also, for single-line [font=monospace]if[/font] statements, I recommend always using {brackets}. It reduces the potential for bugs later on:
if ( isset($argv[1]) && $argv[1] === "name" ){ echo "name = {$argv[1]}\r\n"; }
Mad_T;11003927 wrote:Normally I'd do a simple :
if ($var=="1) {
$a=1;
$b=5;
$c=7;
} else {
$a="No";
}
Could I write that as ternary ?
you could, but it wouldn't really be useful / a good idea (IMO).
$var == 1?
($a = 1) && ($d = 5) && ($c = 7):
($a = "No")
;
ternary statements are really only useful when there is only_one thing to do for each operand (and, there's a good argument for not "nesting" statements as well).
Mad_T;11003927 wrote:The "mysql_connect SERVER A" would be something like :
mysql_connect("localhost","mysql_user","mysql_pwd");
So can I do :
$var = (isset($argv[1])) ? 'mysql_connect("localhost","mysql_user","mysql_pwd");' : 'mysql_connect("remotehost","mysql_user","mysql_pwd");';
yes (again, fixing typos):
$var = ( isset($argv[1]) ) ?
(mysql_connect( "localhost","mysql_user","mysql_pwd" )):
(mysql_connect( "remotehost","mysql_user","mysql_pwd" ))
;
(I've found that indenting ternary statements really helps remind you how they're going to work)