Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
Hey everyone I have this one line of code, and I was thinking about something, is this considered a legal assignment of a variable or will it cause errors. Furthermore is it okay to use exit() statements like this, or am I just terrible at coding somedays? Also if there is a duplicate question like this, please point me in the right direction that would be fanastic!
list($foo, $bar) ? generateValues($data) : exit("Unable to obtain useful information);
The list() you are using will assign $foo and $bar values if you use it like so:
list($foo,$bar) = array('fooValue', 'barValue');
so to properly use it in a tertiary statement would be like so:
list($foo, $bar) = (conditional) ? generateValues($data) : exit('...');
the exit will fire if the conditional is false, otherwise the array generated by generateValues() will be returned by the assignment, and list() will assign the values respectively.
Documentation.
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
I'm PHP developer but i cant understand this error
$uid = $this->db->Tables("telegrambots")->search([
"telegrambotid" => $this->botKey
])['uniqueId'];
if (!file_exists("TelegramBotCommands/{$uid}"))
mkdir("TelegramBotCommands/{$uid}");
Eval is evil, you probably don't need it so don't use it. You want to make a call to a class with a dynamic name? Use this:
$dynamic_class_name = 'Video';
$video = new $dynamic_class_name();
That being said, your snippet with eval seems to work perfectly fine:
http://sandbox.onlinephpfunctions.com/code/e3bb43b1ccfd27365247120e9c5751aac9e2b4ce
You would have to check your logs as to what the error is.
EDIT:
As you said you are using namespaces, try to use the full classname including the namespace in the eval function (like new \namespace\Videos(.... Even better though: don't use eval!
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I am facing some problem in my PHP function.
function pagename($id){
$query=mysql_query("select * from tbl_pages where recid='$id' and langid='$LangID'")
$rs=mysql_fetch_array($query);
$page_name=$rsp['pgname'];
print $page_name;
}
i am not getting any resutl
I saw some problems in your code
First, there is no connection string to your database, I hope you do the connection before to do the request.
Then, in your request, you try to use a variable $LangID not declared in your function, maybe you forgot to put it in your function declaration.
You put the result of your request in the $rs variable and then you try to read the $rsp variable.
You are using mysql in your code, actually it's very unsafe to use it, it's very recommended to use mysqli or PDO instead.
Finally, you don't return anything with your function, you are missing the return statement or maybe you just want to display the result ?
EDIT : I suggest you to write your SQL requests with uppercase, it's more readable for you and other people who read your code.
SELECT * FROM tbl_pages WHERE recid='$id' AND langid='$LangID'
There's no $LangID parameter.
$rs and $rsp are different variables in your code, you should use only one of them.
This function does not return anything, even if you get something in your print.
Check if mysql connection is already established.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I need to access the input value of a dynamic variable based on another input field.
Sample code:
$upid=$_GET['upid'];
$check_box_name='c'.$upid;
echo $upid;
$check=$_GET[$check_box_name];
any idea how do i access it??...Please help
The code you entered should work, but, it is vulnerable to errors, as you are dealing with a user input, you should either do a validation or a failover value.
If you are using PHP5.3+, you can easily do this as follows:
if ($check = #$_GET['c' . (#$_GET['upid'])]? : false !== false) {
//do something with $check
} else {
//failed
}
The # sign is used to omit any error or exception from being thrown. Also, it maybe a good thing to escape the $check variable for more security.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 9 years ago.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Improve this question
I have a little problem with "short if" in Php.
isset($this->sets[$this->value])?$this->sets[$this->value]:$this->value
this drops notices. It looks the $this->sets[$this->value] runs even when it doesnt exists. If I do:
if (!isset($this->sets[$this->value]))
{
$this->sets[$this->value] = '';
}
it does solve the problem, but then I dont understand something....
EDIT: I refactored:
if (isset($this->sets[$this->value]))
{
$value = $this->sets[$this->value];
}
else
{
$value = $this->value;
}
return $value;
and it works, dunno why....
return 'something'.isset($this->sets[$this->value])?$this->sets[$this->value]:$this->value;
'something'.isset($this->sets[$this->value]) always evaluates to true. You'll need to group the ternary operator expression:
return 'something' . (isset($this->sets[$this->value]) ? $this->sets[$this->value] : $this->value);
And that's why you always post a complete example in your question, not just a subset!
It is not true that both operands are evaluated. Try this to see:
true?print('1'):print('2');
Only the '1' prints.
The issue is that your first line of code does not do anything in and of itself. You don't assign the result of the expression to anything, you don't use it anywhere, I would not be suprised if zend just discards it.
In your second example, you explicitly create an array element if it does not already exist. If you wanted to do the same thing with the ternary operator, you could do
isset($this->sets[$this->value])?null:($this->sets[$this->value]='');
I do not know why you would want to, but it would achieve the same thing as you did in your second example.
Your refactored example can be accomplished using the ternary operator as:
return isset($this->sets[$this->value])?$this->sets[$this->value]:$this->value;
This is a typical usage of the ternary operator
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
function myCheck($in)
{ return isset($in); }
$var1='Something';
$var2='$var1';
$var3='$varNonExitant';
What I'm trying to achive is to use myCheck to evaluate the existance of the content like this:
myCheck($var2) return true;
myCheck($var3) return false;
isset() is not really a function: it's a language construct. As such, it's allowed to do some magic that's not available to regular functions, such as being fed with non-existing variables.
To sum up: you cannot replicate it with a custom function.
Edit:
As DaveRandom pointed out in a comment below, all you can do is come close by checking if a variable isset for example:
function variable_isset(&$variable = NULL) {
return isset($variable);
}
This approach offers two drawbacks though:
This works by passing the unset variable by reference, thus creating it when called. As it's NULL it is still not set.
It'll trigger an Undefined variable notice if the variable does not exist, ruining the whole concept of gracefully handling optional variables.
Most likely this should not be needed. So question remains why you can not use isset in the first place which would be much more needed to give you better guidance.
When you cyall myCheck($abc), with set $abc = 123, it gets myCheck(123). It isn't any valid argument for isset.
You have to give the function a string with variable name:
function isset_global($variable_name)
{
return isset($GLOBALS[$variable_name]);
}
Of course, I am also wondering, why to do this, but it answers the question as long as you check a global variable.