Delete an element of array in function - php

I'm using some functions to delete vars. My code is like:
<?
$arr['var1'] = 'Hello';
$arr['var2'] = 'world';
function foo(){
global $arr;
unset($arr['var2']);
}
foo();
But in the PHP manual:
To unset() a global variable inside of a function, then use the
$GLOBALS array to do so:
unset($GLOBALS['arr']['var2']);
Doesn't unset anything because $GLOBALS['arr']['var2'] doesn't exist.
I only want to unset GLOBAL array element inside function.
It exists, because GLOBALS are supervariable and it has everything other var has.
edit:
I tried to do it but after I try to call foo() then i try to print_r($arr)
it show both var1 and var2, and if I try print_r($GLOBALS['arr']['var2']) it show undefined index.... Maybe it's be config...
edit2
I mistyped it in my script. So it's working...
Full working code:
<?
$arr['var1'] = 'Hello';
$arr['var2'] = 'world';
function foo(){
global $arr;
unset($GLOBALS['arr']['var2']);
}
foo();

You can pass variable by reference:
$arr['var1'] = 'Hello';
$arr['var2'] = 'world';
function foo(&$a){
unset($a['var2']);
}
foo($arr);
https://secure.php.net/manual/en/language.references.pass.php

unset($GLOBALS['arr']['var2']); is correct.
see here https://3v4l.org/rCN5h
<?php
$arr['var1'] = 'Hello';
$arr['var2'] = 'world';
function foo(){
unset($GLOBALS['arr']['var2']);
}
var_dump($arr);
foo();
var_dump($arr);

Related

Why is the use language construct in php unable to see changes in the value of a variable?

I have got to the chapter of anonymous functions in php manuel. In the earlier chapter two methods of passing variables to a function are explained, namely, pass by value and pass by reference. Being a javascript developer, it was very unconfortable to see that functions do not have the access to the variables defined in the parent scope. Anyways, now they have come up with a third method to pass variables to a function -- by the use language construct. Please consider the following example:
$message = "hello";
$example = function () use ($message) {
var_dump($message);
};
$example(); //prints hello, as expected.
$message = 'world';
$example(); //prints hello -- not world.
The last call to $example should print "world", but it prints the older value of $message. Why is that? Instead if we had used either pass-by-value or pass-by-reference, the last function call would have printed world.
The use language construct captures/copies the passed variable at the time the anonymous function is defined. If you change that variable after the function definition, the function will not notice it:
$foo = 'baz';
$f1 = function() use ($foo) {
echo $foo;
};
$foo = 'boo';
$f1(); // baz
However, if the variable is an object, this rule doesn't apply, as PHP passes objects by reference*, always (* read Jeto's comment below for a more accurate description):
$foo = new StdClass();
$foo->bar = 'baz';
$f2 = function() use ($foo) {
echo $foo->bar;
};
$foo->bar = 'boo';
$f2(); // boo
I think the reason PHP has use is for partial function application:
function get_multiplier($factor) {
return function($num) use ($factor) {
return $num * $factor;
};
}
$multiply_by_4 = get_multiplier(4);
echo $multiply_by_4(5); // 20

PHP define global variables for functions?

I want to create an array which is in my function.php code definded so I do not have to transfer the array through my hole code. But it won't work...
This is an example of my function.php:
<?php
define("titleArray", array());
function foo(){
echo $this->titleArray;
}
function boo(){
array_push($this->titleArray, "Hello");
}
But it doesn't work... How can I fix that so that every function has access to the array?
Greetings
I suggest that you store your array globally using the $GLOBALS, and instead of using echo to print an array, use the print_r method instead.
<?php
$GLOBALS["titleArray"] = array('world');
function foo(){
print_r($GLOBALS["titleArray"]);
}
function boo(){
array_push($GLOBALS["titleArray"], "Hello");
print_r($GLOBALS["titleArray"]);
}
foo();
boo();
just remove $this
<?php
define("titleArray", array());
function foo(){
echo titleArray;
}
function boo(){
array_push(titleArray, "Hello");
}
note that array values are allowed after php 7.0 version
Generally we use define to make constant value which never updates, use class instead
<?php
class abc {
$titleArray = [];
function foo(){
echo $this->titleArray;
}
function boo(){
array_push($this->titleArray, "Hello");
}
}
The define() function defines a constant. Constants are much like variables, except for the following differences: A constant's value cannot be changed after it is set.
There are two ways to do it:
use global in every function
declare $GLOBALS[] once outside the function
$titleArray = [];
// $GLOBALS['titleArray'] = [];
function foo(){
global $titleArray;
print_r($titleArray);
// print_r($GLOBALS['titleArray']);
}
function boo(){
global $titleArray;
array_push($this->titleArray, "Hello");
// array_push($GLOBALS['titleArray'], "Hello");
}

Assigning a function to the variable without calling this function

I want to assigning a function to the variable without calling this function when assigning it to variable.
function abc(){
echo 'abc';
}
and in PHP
$variable=abc();
and then echo variable with function
echo $variable;
EDIT*
Because I want to have variable like this
$variable = $something[0].$function.$something[1];
What you want to use is called Variable functions in PHP and is done like this.
function foo() {
return "Hello I am foo()\n";
}
$func = 'foo';
echo $func(); // This calls foo()
See the PHP Manual page for Variable Functions
Try this:
$variable = function() {
echo 'abc';
};
And then call it like:
$variable();
demo
do like this
$variable=function(){
echo 'abc';
};
$variable();
or
function abc(){
echo 'abc';
}
$variable="abc";
$variable();
Seems like you want to capture what is being echoed (are you using Wordpress?):
ob_start();
abc();
$variable = ob_get_clean();
echo $variable;
Starts a buffer to hold the output and then gets the output from the buffer.

Global scope not printing data

Why is $a not printing?
And what is the alternate of this, and I dont want to use return.
function abc () {
$a = 'abc';
global $a;
}
abc();
echo $a;
The reason why it's not echoing is because of two things:
1) You need to declare global "before" the variable you wish to define as being global.
and
2) You also need to call the function.
Rewrite:
<?php
function abc()
{
global $a;
$a = 'abc';
}
abc();
echo $a;
For more information on variable scopes, visit the PHP.net website:
http://www.php.net/manual/en/language.variables.scope.php
You can get your variable as:
echo $GLOBALS['a'];
see http://php.net/manual/en/language.variables.scope.php
You can use define():
function abc() {
define("A", "abc");
}
abc();
echo A;
Make sure you call the function. I added that just above echo.
First you must create and assign a variable. And then in you function describe that is a global var you want to use.
$a = 'zxc';
function abc() {
global $a;
$a = 'abc';
}
abc();
echo $a;
This is not really good idea to use golbal such way. I don't really understand why I so much want to use a global var...
But my opinion is better for you to use a pointer to variable.
function abc(&$var){
$var = 'abc';
}
$a = 'zxc';
abc(&$a);
echo $a;
Or even would be better to create an object and then access variable with-in this object

Inside function variables to be used in includes in PHP

I have a function that includes another file like so
// some function
function SomeFunction()
{
$someData = 'SomeData';
include_once('some_file.php');
}
// some_file.php
<?php echo $someData; ?>
How would I get this to work where the include file can use the variables from the calling function? I will be using some output buffering.
As long as $someData is defined in SomeFunction(), some_file.php will have access to $someData.
If you need access to variables outside of SomeFunction(), pass them as arguments to SomeFunction().
That's already available :)
See include()
The best would be to not do use globals at all, but pass the variable as parameter:
function SomeFunction()
{
$someData = 'SomeData';
include_once('some_file.php');
some_foo($someData);
}
Otherwise you risk transforming your code base in spaghetty code, at least on the long term.
Seems kinda unorganized to include files in functions...what about...
function SomeFunction()
{
$someData = 'SomeData';
return $someData;
}
$data = SomeFunction();
<?php include('file.php') ?> // file.php can now use $data
You don't have to do anything. Usage of include() (and it's siblings) is analogous to copy-pasting the code from the included file into the including file at the spot where include() is called.
Simple example
test.php
<?php
$foo = 'bar';
function test()
{
$bar = 'baz';
include 'test2.php';
}
test();
test2.php
<?php
echo '<pre>', print_r( get_defined_vars(), 1 ), '</pre>';
Again, this is analogous to the combined
<?php
$foo = 'bar';
function test()
{
$bar = 'baz';
echo '<pre>', print_r( get_defined_vars(), 1 ), '</pre>';
}
test();

Categories