function report_test() {
$color = "blue";
$name = "John";
mobile_test($color, $name);
echo $columnid;
}
function mobile_test($color, $name) {
-snipped Insert MySQL Query-
$columnid = 5;
return $columnid;
}
Hopefully the example above portrays what I'm attempting to do. Globalising the variable columnid isn't an option in this scenario due to other various reasons. I also do not want to alter the mobile_test($color, $name) function. I find that if I echo the function instead the MySQL Insert Query is ran twice, meaning two sets of the same results gets entered into the database.
Is there another way to do this?
I assume that you want to echo out the return value from mobile_test() inside of report_test(). The easiest way to do this is to simply echo out $columnid inisde of mobile_test(). When you call mobile_test() from report_test(), the echo statement will be called as well, and the value will be outputted:
function mobile_test($color, $name) {
...
$columnid = 5;
echo $columnid;
return $columnid;
}
There's also the possibility to make use of PHP's short tag syntax (as <?= mobile_test(); ?>), assuming you want to echo the return of the function directly, and not do anything else with it.
Note that if you leave in the return value (return $columnid;), you'll be able to use this value directly as a comparison in the report_test() function:
if (mobile_test($color, $name) === 5) {
echo "The column ID is 5" /* This line will be triggered */
}
You may do this:
function report_test() {
$color = "blue";
$name = "John";
$columnid = mobile_test($color, $name);
echo $columnid;
}
you need to store the returned value from mobile_test() as a new variable. This won't need to change the mobile_test()
Related
So I have this array:
$myArray = array('myVar', 'myVar75', 'myVar666');
How do I isset check whether the variable called $myVar,$myVar75,$myVar666 exists or not?
What is the most sensible way of passing the array's value into the isset() function as a variable name to check?
Just use variable variables to test each element with a simple foreach loop:
Example:
$myVar666 = 1; // for example's sake
$myArray = array('myVar', 'myVar75', 'myVar666');
foreach($myArray as $element) {
if(isset(${$element})) {
echo $element, ' is already set';
} else {
echo $element, ' is not yet set';
// if not set, do something here
}
}
Should yield something like this:
$myVar is not yet set
$myVar75 is not yet set
$myVar666 is already set
foreach($myArray as $value){
if(isset($$value)){
echo "$value is exist with value : '".$$value."'";
}
else{
"$value is not exist";
}
}
You'd do that by putting an extra $, as follows:
$myArray = array("myVar", "myVar75", "myVar666");
if (isset($$myArray[0])) {
//Content
}
That would check if $myVar is set. Change the index to myArray accordingly.
The way to think about PHP's implementation of this (called variable variables) is that the content of the string goes after the dollar sign:
$hi = "hello";
But if you put $$hi, then the $hi is replaced with the string's content, so it becomes $hello.
$hi = "hello";
$hello = "greetings";
If you put $$$hi, then the $hi is replaced so that it becomes $$hello, and the $hello is replaced so that it becomes $greetings.
I have just written a program to check for the variable scopes in PHP. The code goes like this:
<?php
$value = 1;
function change_value(){
if(some_condition){
$value = 0;
$asset = 1;
}else{
$asset = 0;
}
return $asset;
}
echo $value;
change_value();
echo $value;
?>
Now, the output of the above program is 11.
How can I change the value of $value once it enters the function change_value() ?
Pass the parameter by reference:
<?php
$value = 1;
function change_value(&$value){
if(/* some_condition */){
$value = 0;
$asset = 1;
}else{
$asset = 0;
}
return $asset;
}
echo $value; // echoes 1
$asset = change_value($value);
echo $value; // echoes 0
echo $asset; // echoes 0 or 1 depending on /* some_condition */
?>
Please don't use global ... even if some suggest it.
It's bad. It let's the variable be accessable from all over the script and you will be very confused when you come upon the situation where you access $value in a different script you included and it acts different then...
What you're trying to do, goes against common principles, but.
$GLOBALS['value'] = 0;
Using this inside function will let you change that value, but I suggest you not to do this.
The way others have outlined, is far more right.
Let's say I have this:
function data() {
$out['a'] = "abc";
$out['b'] = "def";
$out['c'] = "ghi";
return $out;
}
I can output the data by declaring it as a variable, then using the array index to echo it:
$data = data();
echo $data['a'];
echo $data['b'];
echo $data['c'];
But, I'm calling functions inline with other functions, and I'm trying to avoid having to declare a variable first. For instance, I want to do something like this:
echo data()[0]; //pulls first value in array without declaring it as a variable first. This needs to be variable i.e. data()[1] data()[2] etc.
Or more specifically, I'm actually trying to do it as a class:
$traverseXML->getData("Route", "incoming", "field", "value")[0]
//getData() returns an array, I'm trying to get a single value.
Personally i would do something like this
<?php
function data($key = false, $default = 'not found') {
$out['a'] = "abc";
$out['b'] = "def";
$out['c'] = "ghi";
if($key)
{
if(isset($out[$key]))
return $out[$key];
else
return $default;
}
else
return 'empty';
}
?>
<?= data('a') ?>
Here is how I set my Array:
$Post_Cat_Array = array();
while($row = mysql_fetch_array( $result )) {
$Post_Cat_Array[$row['type_id']] = $row['type_name'];}
in this function I need to get the type_id(key) of a specific type_name(value)
function NameToID($input){
echo array_search($input, $Post_Cat_Array);
}
and I call the function like this :
NameToID($_POST['type']);
But it's not working. It doesn't echo anything. I am sure the $_POST['type'] contains correct value.
note:value of $_POST['type'] is in arabic. same with all values of the array.
It seems that $Post_Cat_Array is out of scope. Modify your function:
function NameToID($input, $arr){
echo array_search($input, $arr);
}
and then:
NameToID($_POST['type'], $Post_Cat_Array);
From PHP Variable scope:
This script will not produce any output because the echo statement
refers to a local version of the (...) variable, and it has not been
assigned a value within this scope.
That is because your array variable is not known to your function. You can use either of the following to achieve that
<?php
$Post_Cat_Array=array();
$Post_Cat_Array["key1"]="value1";
$Post_Cat_Array["key2"]="value2";
$Post_Cat_Array["key3"]="value3";
$Post_Cat_Array["key4"]="value4";
echo NameToID("value4");
echo "<br>";
echo NameToID2("value4",$Post_Cat_Array);
function NameToID($input){
global $Post_Cat_Array;
echo array_search($input, $Post_Cat_Array);
}
function NameToID2($input,$values){
echo array_search($input, $values);
}
?>
<?php
class obj {
var $primary_key;
function obj($key = null){
if(isset($key)){
$this->primary_key = $key;
}
echo "primary_key: ".$this->primary_key."<br/>";
$this->obj_id = 14;
echo "obj_id: ".$this->obj_id."<br/>";
$key = $this->primary_key;
echo "obj_id from primary_key string: ".$this->$key."<br/>";
}
}
?>
Is there a way to get $this->primary_key value?
Like $this->$this->primary_key?
<?php
class obj {
var $primary_key;
function obj($key = null){
if(isset($key)){
$this->primary_key = $key;
}
echo "primary_key: ".$this->primary_key."<br/>";
$this->obj_id = 14;
echo "obj_id: ".$this->obj_id."<br/>";
$key = $this->primary_key;
echo "obj_id from primary_key string: ".$this->$key."<br/>";
}
}
?>
$aaa = new obj("obj_id");
i want to get $this->obj_id value, but i dont whant to use variable $key.
This line $this->obj_id = 14; will be dynamic and i would't be able to know,
if $this->obj_id will be named obj_id or something else like $this->banana, it would be desided by new obj("banana")
Well in order for that function to even be of any use, you would need to create a new class and set it to a variable, such as this:
$myclass = new obj(1);
Although that form of a constructor is only supported for backward compatibility (deprecated, you're really supposed to use __construct now), it should still run and set all your variables, which can be retrieved in the following way:
echo $myclass->primary_key; // the primary_key value
echo $myclass->obj_id; // the obj_id value
Edit: Also this line needs to be corrected to change the '$key' to 'primary_key' near the end. Putting '$key' there will not return anything because it would be sending '$this->1' which does not exist.
echo "obj_id from primary_key string: ".$this->primary_key."<br/>";
$obj_a = new obj(5);
echo $obj_a->primary_key; // prints '5'
In your last line:
echo "obj_id from primary_key string: ".$key."<br/>";
or:
echo "obj_id from primary_key string: ".$this->$primary_key."<br/>";
Seriously, I still don't understand the utility of this code...