This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
php variable = variable1 || variable2
Trying to do this in PHP evaluates to true, instead of returning 'nothing' like js does.
//Javascript
var stuff = false;
document.write(stuff || 'nothing');
So I have to do this. Is there anyway to avoid typing the variable stuff twice?
//PHP
$stuff = false;
echo !empty($stuff)?$stuff:'nothing';
If you use PHP 5.3 or later you can use the shorthand ternary format:
echo ($stuff) ?: 'nothing';
Related
This question already has answers here:
What are the PHP operators "?" and ":" called and what do they do?
(10 answers)
How to check if $data variable is set with Codeigniter?
(6 answers)
Closed 4 years ago.
Hey I'm new to php and codeigniter. I know codeigniter has an isset function.
what does the following code mean? Can someone please help
<?php echo isset($error) ? $error : ''; ?>
isset is a php function, you can use it without CodeIgnitor, but it basically checks to see if the variable has been set yet.
$someVariable = 'This variable has been set';
var_dump(isset($someVariable)); // True
var_dump(isset($anotherVariable)); // False
the ? and : parts tell PHP what to do. It's called a ternary operator, and can be thought as as a short if statement:
echo isset($someVariable) ? 'set' : 'not set';
is the same as:
if (isset($someVariable)) {
echo 'set';
} else {
echo 'not set';
}
http://php.net/manual/en/function.isset.php
http://php.net/manual/en/language.operators.comparison.php#language.operators.comparison.ternary
This question already has answers here:
How can I get useful error messages in PHP?
(41 answers)
Closed 8 years ago.
I was wondering if it's possible to do something like:
for ($d=0; $d<20; $d++) {
$productname.$d = $_POST['productname'.$d];
$link.$d = $_POST['link'.$d];
$color.$d = $_POST['color'.$d];
$size.$d = $_POST['size'.$d];
$otherinfo.$d = $_POST['otherinfo'.$d];
$no.$d = $_POST['no'.$d];
$other.$d = $_POST['other'.$d];
}
since the code above doesn't work.
What am I doing wrong? Any help is appreciated.
Your $productname.$d statement has no sense.
Use $productname[$d] instead. It is array approach and it is much more preferable.
P.S.: If you really want so many different variables you can use variable variables (pseudocode is below):
$varName = 'productname'.$d;
$$varName = $_POST['productname'.$d];;
This question already has answers here:
How do I immediately execute an anonymous function in PHP?
(9 answers)
Closed 10 years ago.
Is it possible to do something like this. Lets say we have a function that accepts string as argument. But to provide this string we have to do some processing of data. So I decided to use closures, much like in JS:
function i_accept_str($str) {
// do something with str
}
$someOutsideScopeVar = array(1,2,3);
i_accept_str((function() {
// do stuff with the $someOutsideScopeVar
$result = implode(',', $someOutsideScopeVar); // this is silly example
return $result;
})());
The idea is to when calling i_accept_str() to be able to directly supply it string result... I probably can do it with call_user_func which is known to be ineffective but are there alternatives?
Both PHP 5.3 and PHP 5.4 solutions are accepted (the above wanted behavior is tested and does not work on PHP 5.3, might work on PHP 5.4 though...).
In PHP (>=5.3.0, tested with 5.4.6) you have to use call_user_func and import Variables from the outer Scope with use.
<?php
function i_accept_str($str) {
// do something with str
echo $str;
}
$someOutsideScopeVar = array(1,2,3);
i_accept_str(call_user_func(function() use ($someOutsideScopeVar) {
// do stuff with the $someOutsideScopeVar
$result = implode(',', $someOutsideScopeVar); // this is silly example
return $result;
}));
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Checking if an associative array key exists in Javascript
I have a PHP code block . For a purpose I am converting this to a JavaScript block.
I have PHP
if(array_key_exists($val['preferenceIDTmp'], $selected_pref_array[1]))
now I want to do this in jQuery. Is there any built in function to do this?
Note that objects (with named properties) and associative arrays are the same thing in javascript.
You can use hasOwnProperty to check if an object contains a given property:
o = new Object();
o.prop = 'exists'; // or o['prop'] = 'exists', this is equivalent
function changeO() {
o.newprop = o.prop;
delete o.prop;
}
o.hasOwnProperty('prop'); //returns true
changeO();
o.hasOwnProperty('prop'); //returns false
Alternatively, you can use:
if (prop in object)
The subtle difference is that the latter checks the prototype chain.
In Javascript....
if(nameofarray['preferenceIDTmp'] != undefined) {
// It exists
} else {
// Does not exist
}
http://phpjs.org/functions/array_key_exists:323
This is a great site for PHP programmers moving to js.
This question already has answers here:
What is ?: in PHP 5.3? [duplicate]
(3 answers)
Closed 8 years ago.
So I'm using the following php code to set variables that are received from a POST method, but I'm interested in how it works.
$var1 = isset($_REQUEST['var1']) ? $_REQUEST['var1'] : 'default';
I understand what it does, but I don't understand the syntax.
Thanks for the help :)
? is just a short and optimised notation of doing this:
if (isset($_REQUEST["var1"])) // If the element "var1" exists in the $_REQUEST array
$var1 = $_REQUEST["var1"]; // take the value of it
else
$var1 = "default"; // if it doesn't exist, use a default value
Note that you might want to use the $_POST array instead of the $_REQUEST array.
This is a short hand IF statement and from that you are assigning a value to $var1
The syntax is :
$var = (CONDITION) ? (VALUE IF TRUE) : (VALUE IF FALSE);
You probably mean ternary operator
Syntax it's same like
if(isset($_REQUEST('var1') ) {
$var1 = ? $_REQUEST('var1')
}else {
$var1 =: 'default';
}
It's the synatx of the ternary operator. It's shorthand for if/else. Please read PHP Manaul
This is a 'ternary operator', what it says is:-
If var1 is set as a post variable then set var1 to that value, otherwise setvar1 to be the string 'default'. Using traditional syntax it would be:-
if (isset($_REQUEST('var1')) { $var1 = $_REQUEST('var1'); } else { $var1 = 'default'; }
its a short way of doing an if. if you are expecting a post variable its must better to use _POST rather than request.
the "?" says if the isset($_REQUEST) is true, then do the everything between the ? and : otherwise do everything between the : and the ;