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
This are two lines of codes. Could anyone tell me what the difference is between the first way and the second? I'd like both to do exactly the same thing.
$test = isset($_POST['test'])?$_POST['test']:[];
if(isset($_POST['test'])){
$test[] = $_POST['test'];
}
Thanks !
First one sets $test to an empty array if $_POST['test'] is unset. However, the second one does not set $test to a default value. In fact, if $_POST['test'] was unset, $test would be un-existent/undefined/etc.
You would need to run $test = []; at the beginning of the second one to archive the exact same result.
the top line would be equivalent to
if(isset($_POST['test'])){
$test = $_POST['test'];
}else{
$test = [];
}
The first uses a ternary operator, which is a shorthand for if (X) then $a = b else $a = c, which sets $test to $_POST['test'] if it isn't empty, or $test to an empty array.
The second example doesn't have the else case, so it will leave $test undefined if $_POST['test'] is empty.
See also the ternary operator section of this page in the PHP manual http://www.php.net/manual/en/language.operators.comparison.php.
Related
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 8 years ago.
Improve this question
I was wondering if you could give two values to a class and then acces to the second one by POST, something like this (part of the code):
echo "<select name='selecttsk' id='selecttsk'>";
while($w = $bd->obtener_fila($tasker, 0)){
echo "<option class='opcion1' value ='".$w[1]/$w[2]."'>".$w[0]."</option>";
}
echo "</select>";
?>
and then i need to do something like this in other file
$var = $_POST[selecttsk];
but i need $w[2]
thanks
I suppose your $_POST['selecttsk'] does have the values in the following format:
"foo/bar"
You could use "explode" in PHP to get the second part (bar), for example:
$postvar = $_POST['selecttsk'];
$vars = explode("/", $postvar);
// Then
$var = $vars[1]; // Will be the $w[2] from the form
Look into: http://php.net/explode
Beware that if $w[1] or $w[2] ever contains a "/" you might get unexpected results, you could use the limit function of explode to mitigate that issue.
However - I would generally not recommend this workflow.
Why do you need to send two variables with one select?
(could you show us som example of what $w contains)
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 8 years ago.
Improve this question
Generally we add to an array with
$myarray[] = $myItem
but if $myItem is a massive object I don't want it to get copied, instead I want it to be assigned by reference. Something like
$myarray[] = &$myItem
but that doesn't work and instead replaces every element in $myarray with $myItem
I tried
$myarray[count($myarray)] = &$myItem
but it still replaces everything in $myarray
I am running PHP v5.5
Objects are always assigned by reference. So:
$collection = array();
$blah = new Blah();
$blah->param = "something";
$collection[] = $blah;
$blah->param = "changed";
echo $collection[0]->param; // will output "changed"
According to How to push a copy of an object into array in PHP
Objects are always passed by reference in php 5 or later.
Therefore this question isn't really a concern anymore.
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
Ive asked a similar question before but i was really unclear so ive decided to use a more concrete example.
Does php save the result of the variable or does it save the procedure to run it? Why im wondering is if i store a function in it, does it store the return value or just copies the procedure
say:
function foo($something)
{
for loop
{
echo 'Something';
}
return $something;
}
$b = foo(5);
from what i encountered just assigning the value executes the function. Which i dont want because i dont want to go through double the for loops and do double what could be inside.
In PHP you can have both (either store result, or function's code)
if you write:
function foo()
{
return 5;
}
$a = foo();
this will mean - execute function foo and store result into $a
if you write:
$a = function()
{
return 5;
};
$a();
this will mean - store function's code into variable $a, then execute function stored in $a
PHP is a strict programming language, meaning that expressions are always completely evaluated. The line
$b = foo(5);
computes the value for foo(5) before the assignment; PHP does not leave it as a thunk to be evaluated when or if the variable $b is used.
If you want to you can achieve something similar to a thunk by creating a closure, like this:
$b = function() { return foo(5); };
This will not evaluate foo(5) until its value is needed, and then to get the value you must call the closure as $b().
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
<?php
$str = 'Hello world!';
echo $str;
unset($str);
?>
Question:
When do I need to use unset()? For the above script, I think that is not necessary to use it. So I just wonder in what situation need to use it?
i mostly use it when deleting sessions...
unset($_SESSION['user']);
Lets say you have an array:
$test = array(
'one' => 1,
'two' => 2,
'three' => 3
);
if you don't need three anymore easily you can do unset($test['three']); if you don't have unset, how would you do it?
Try this code
<?php
$str = 'Hello world!';
$myvar = 'another var';
$params = array($str,$myvar);
myunset($params);
function myunset($params){
foreach($params as $v){
unset($GLOBALS[$v]);
}
}
var_dump($myvar); //NULL
?>
This is a question about PHP garbage collection, first we need to know that:
PHP performs garbage collection at three primary junctures:
When you tell it to
When you leave a function
When the script ends
http://www.tuxradar.com/practicalphp/18/1/10
You can follow the link above to get more information, my suggestion is that you can use unset when processing a big resource.
Unset function actually use for unset the value of any variable you can do this
unset($var);
unset($_SESSION['logout']);
unset($row['firstname']);
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.