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']);
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 8 years ago.
Improve this question
I have a little question about variables in php.
How can I make this possible??
<?php
echo $test;
$test = 'This is a test';
?>
I know that you could fix it with ease with
<?php
$test = 'This is a test';
echo $test;
?>
but I cant use it in my page in that way.
Can any one please tell me how I can have a variable after the echo??
Buddy.. It can't be Done.. How you can output a value of a variable when its not assign to it..
Its common sense..
<?php
echo getTest();
function getTest(){
return 'This is a test';
}
You can't. A variable has to be declared before it has a value.
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.
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 1 year ago.
Improve this question
I want to avoid writing to DB and use constants/array for lang files etc.
i.e:
$lang = array (
'hello' => 'hello world!'
);
and be able to edit it from the back office.
(then instead of fetching it from the poor db, i would just use $lang['hello']..).
what are you suggesting for the best and efficient way to pull it of?
the most efficient way i found looks like this:
build up your array in php somehow and export it into a file using var_export()
file_put_contents( '/some/file/data.php', '<?php return '.var_export( $data_array, true ).";\n" );
then later, wherever you need this data pull it like this
$data = include '/some/file/data.php';
Definitely JSON
To save it :
file_put_contents("my_array.json", json_encode($array));
To get it back :
$array = json_decode(file_get_contents("my_array.json"));
As simple as that !
Well if you insist to put the data into files, you might consider php functions serialize() and unserialize() and then put the data into files using file_put_contents.
Example:
<?php
$somearray = array( 'fruit' => array('pear', 'apple', 'sony') );
file_put_contents('somearray.dat', serialize( $somearray ) );
$loaded = unserialize( file_get_contents('somearray.dat') );
print_r($loaded);
?>
You can try json_encode() and json_decode().
$save = json_encode($array);
write contents of $save to file
To load lang use:
$lang = file_get_contents('langfile');
$lang = json_decode($lang, true);