I have recently been employed at an office where they use a lot of php in their work, most of my development background is HTML, CSS, Jquery, Wordpress and Angularjs, I have an idea behind the logic of some of php but was just wondering if anyone can enlighten me as to what this code below actually means/does?
return (isset($rs[0][0]) ? $rs[0][0] : "");
It is located within this function that calls the database and returns values.
function get_temp($table, $field){
global $db;
$sql="select $field from $table";
$rs=$db->select($sql);
return (isset($rs[0][0]) ? $rs[0][0] : "");
}
I feel that is selecting one value from an array within an array but I cannot find any sources to confirm this so was hoping someone on here could help me out or at least point me in the right direction if I am wrong. The reason I believe this is the case is because if I pass the $field variable more than one result it will always only return the first one, if this is the case it would also be helpful to me if someone could suggest a way to get all of the results, whenever I simply try:
return $rs
It simply returns "Array".
(isset($rs[0][0]) ? $rs[0][0] : "");
This is a ternary operator. It does a check if $rs[0][0] is set. If yes it will make the function return $rs[0][0]'s value, if not it will return an empty string.
You could translate it to an if statement like this:
if (isset($rs[0][0])) {
return $rs[0][0];
}
return "";
In fact the method isset will check is the value is null.
Then it is simply a ternary operator, returning the value of the variable if it is not empty and an empty string if the value of the var is empty.
Related
I find it's really hard to understand the Null datatype in PHP. Why does it even need to exist? If it's only represent an empty value why PHP doesn't just offer a way to remove it from the code since the variable contains it doesn't have any useful thing to do in our application?
Consider this:
if (some_condition) $var1 = "Bob";
Now if that condition is false, then $var1 would be null because you did not define it. So, you could have the decision to check if it is null to proceed with what you want to do with it.
if (!is_null($var1)) {
// do something
}
Hope that helps.
I have a condition stored in a variable $condition = "1==1" and I want to use the value of the variable in a conditional statement.
if($condition) { //$condition should be parsed as 1==1
return true;
}
Is this possible? I've tried using var_export but apparently that doesn't work.
if(var_export($condition)) {
return true;
}
Any help will be greatly appreciated!
After some discussion in the comments above we found out, that the condition itself is not the problem, but how it is passed to the function
conditional_func($var . " == 'test'", $result);
We realized, that it is not required to pass the first parameter as a string, but can use a boolean directly, thus we don't need to evaluate the string at all.
conditional_func($var == 'test', $result);
As a sidenote: #BryanMoyles answer is right regarding the question, but remember, that eval() is evil (you can't think of any pitfall, that may occur) and on the other hand there are only some very less quite esoteric usecases, where you can't use another approach.
I have a pretty nasty error I can't get rid of. Here's the function causing the issue:
function get_info_by_WatIAM($WatIAM, $info) {
$users_info = array();
exec("uwdir -v userid={$WatIAM}", $users_info);
foreach ($users_info as $user_info) {
$exploded_info = explode(":", $user_info);
if (isset($exploded_info[1])){
$infoArray[$exploded_info[0]] = $exploded_info[1];
}
}
return $infoArray[$info]; }
Here's what's calling the function:
} elseif ( empty(get_info_by_WatIAM($_POST['ownerId'])) ) { ...
I would really appreciate any suggestion. Thanks very much!
If the code doesn't make sense, here's a further explanation: exec uses a program that stores information on all the users in a school. These include things like faculty, name, userid, etc. The $_POST['ownerId'] is a username -- the idea is that, upon entering a username, all of the user's information is automatically filled in
You do not need empty around function calls, in fact empty only works with variables and not functions (as you see). You only need empty if you want to test a variable that may not be set for thruthiness. It is pointless around a function call, since that function call must exist. Instead simply use:
} else if (!get_info_by_WatIAM($_POST['ownerId'])) { ...
It does the same thing. For an in-depth explanation, read The Definitive Guide To PHP's isset And empty.
empty can only be used on variables, not on expressions (such as the result of calling a function). There's a warning on the documentation page:
Note:
empty() only checks variables as anything else will result in a parse
error. In other words, the following will not work: empty(trim($name)).
Just one of PHP's best-left-alone quirks.
One workaround is to store the result in a variable and call empty on that, although it's clunky. In this specific case, you can also use
if (!get_info_by_WatIAM(...))
...although in general, if (empty($a)) and if(!$a) are not equivalent.
get the value of this
$a = get_info_by_WatIAM($_POST['ownerId'])
then chack
empty($a)
it will work
PHP newbie here
Can anyone please tell me what is wrong with the below syntax. I have a maximum of 4 files - $created_page1, $created_page2 each with a corresponding page title etc and would like to process these in a loop. However PHP throws a wobbly every time I try to concatenate the string and loop number - specifically $created_page.$num_pages doesn't result in sending $created_page1 or $created_page2 to the function, instead it just converts the string and number to an integer. Very basic I am sure but I would be very grateful for any help or a nicer solution that I can easily understand. Thanks in advance!
$addit_pages == 4;
for ($num_pages=1;$num_pages<=$addit_pages ;$num_pages++) {
replaceFileContent ($dir,$created_page.$num_pages,"*page_title*",$page_title.$num_pages);
//replaceFileContent ($dir,$created_page2,"*page_title*",$page_title2);
//replaceFileContent ($dir,$created_page1,"*page_title*",$page_title3);
//replaceFileContent ($dir,$created_page3,"*page_title*",$page_title4);
}
Your code to get the variable name should be:
${'created_page'.$num_pages}
This is because you have to evaluate the string inside the braces before you attempt to access the variable.
Your previous code was trying to access the variables $created_page and $num_pages, and simply concatenate their values into a string.
Of course, the same goes for the page_title variable
${'page_title'.$num_pages}
you could try this:
$addit_pages == 4;
for ($num_pages=1;$num_pages<=$addit_pages ;$num_pages++) {
replaceFileContent ($dir,$created_page.strval($num_pages),"*page_title*",$page_title.strval($num_pages));
//replaceFileContent ($dir,$created_page2,"*page_title*",$page_title2);
//replaceFileContent ($dir,$created_page1,"*page_title*",$page_title3);
//replaceFileContent ($dir,$created_page3,"*page_title*",$page_title4);
}
the PHP strval function makes any integer into a string
I think what you are asking is you want the variables $created_page1; $created_page2, $created_page3 but php is probably throwing a notice that $created_page doesn't exist.
You need to use variable variables (is this what they're called?)
$addit_pages == 4;
for ($num_pages=1;$num_pages<=$addit_pages ;$num_pages++) {
$createdVar = 'created_page'.$num_pages;
$titleVar = 'page_title'.$num_pages;
replaceFileContent ($dir,$$createdVar,"*page_title*",$$titleVar);
}
When you use $$ this first evaluates the variable $createdVar turns that into created_page1 and then evaluates created_page1 as if you had typed in $created_page1
Can someone please explain to me the following line of php?
($myobjectfunction = object::function('letsgo')) ? eval($myobjectfunction) : false;
I understand objects and their functions. Is this php saying if $myobjectfunction is defined then eval $myobjectfunction, otherwise do nothing? Because in the code I am reading, object hasn't yet been defined before this line (sometimes).
This line assigns the returned value from the function object::function('letsgo') to the variable $myobjectfunction. If the return was a "truthy" value (evaluates to boolean TRUE), the contents of that variable are then evaluated as PHP code (eval). If the initial return was FALSE, no further action is taken. The false at the end basically does nothing.
This works because PHP will return the value from an assignment, even though it isn't usually used for anything. In the case of your bit of code, however, the return from the assignment is used to determine which branch of the ternary operator to take since it is enclosed in parentheses.
$x = 3;
// returns 3 even if we don't use it...
This is an unusual idiom, because the parentheses are around the initial assignment.
($myobjectfunction = object::function('letsgo')) ? eval($myobjectfunction) : false;
//^^---------------------------------------------^^^
A more typical usage of the ternary operator would assign the output of either side of the ? to the variable on the left, based on the condition to the right of the = like:
$myobjectfunction = object::function('letsgo') ? $someothervalue : false;
It's difficult to tell exactly what's going on here. I'm assuming you have substituted actual values in order to 'simplify' the example, but the use of keywords actually clouds the matter.
The declaration of the class 'object' doesn't need to be before this statement, so long as the object class is defined at some point during the code execution.
This code is equivalent to:
$myobjectfunction = object::function('letsgo');
if( $myobjectfunction ) {
eval( $myobjectfunction );
}
else {
false;
}
In other words, assign the result of object::function( 'letsgo' ) to a variable. If that variable is "truthy" (i.e. not false, null, or 0, or another value that evaluates like false) then eval its contents; otherwise do nothing.
ya you pretty much got it, well its saying IF $myobjectfunction has successfully been returned a positive result (ie: not false, 0, or null) the eval the new variable object, but i probably wouldnt use "false" in the else bit., id probaby use null.
Now for this to do anything, "object" does need to be defined
this is a strange piece of code though, in my own honest opinion