note 35141 deleted from language.operators.bitwise by sniper

From: Date: Mon, 13 Oct 2003 00:13:36 +0000
Subject: note 35141 deleted from language.operators.bitwise by sniper
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-58415@lists.php.net to get a copy of this message
Note Submitter: cancer@cyberdomegt.com ---- There are times when you build strings based on variables, like for a database query, and sometimes, some of the vars you want to use could be not set or empty: Let's supose that our numeric vars aren't set and let's build a query: $query = "insert into table values ( $var1, '$var2', $var3 )"; The result will be something like: $query = "insert into table values ( , 'blah blah', )"; Specially the numeric vars: if they aren't set, we could get a dbengine error... So we first put some lines of code to check for the variables: If some of our vars aren't set, we'll set them to defaults. if ( !isset($var1) ): $var1 = 0; endif; if ( !isset($var2) ): $var2 = "nothing"; endif; if ( !isset($var3) ): $var3 = 1; endif; $query = "insert into table values ( $var1, '$var2', $var3 )"; Now... there's a way to check yours vars on the fly with the | (OR) operator: // The syntax used is: ($variable|<default_value>) $query = "insert into table values ( ".($var1|0).", '".($var2|"nothing")."', ".($var3|1)." ); With the last line, PHP will evaluate if the variable is set or not empty before spitting out either the vars' value or your default value... Hope this helps someone :D

« previous php.notes (#58415) next »