I'm not sure how to write this. I have a variable called $group the value could be a,b or c. I also have variables called $a, $b and $c. How do I write something that states if $group=a then use $a, if $group=b then use $b and if $group=c then use $c. Hope that makes sense.
Do you want this ??
$a = "a"; // $a is variable and "a" is its value and its a string.
$b = "b";
$c = "c";
if ($group == "a") // here a is just string value not variable.
{
echo $a;
}
elseif ($group == "b")
{
echo $b;
}
else
{
echo $c;
}
Or
Variable of variables
Sometimes it is convenient to be able to have variable variable names. That is, a variable name which can be set and used dynamically. A normal variable is set with a statement such as:
<?php
$a = 'hello';
?>
A variable variable takes the value of a variable and treats that as the name of a variable. In the above example, hello, can be used as the name of a variable by using two dollar signs. i.e.
<?php
$$a = 'world';
?>
At this point two variables have been defined and stored in the PHP symbol tree: $a with contents "hello" and $hello with contents "world". Therefore, this statement:
<?php
echo "$a ${$a}";
?>
produces the exact same output as:
<?php
echo "$a $hello";
?>
i.e. they both produce: hello world.
For more info click here
With Rahul's help and a tiny bit of tweaking this was my working end result.
if ($group == "a")
{
$group=$a;
}
elseif ($group == "b")
{
$group=$b;
}
else
{
$group=$c;
}
Related
$a = 'i am a $b'; // declared before $b is declared
function x(....) {
global $a;
$b = 'boy';
$c = '{$a}'; // i know this doesn't work. how can I make it work?
}
I want $c to return "i am a boy"
This is a simple example of my issue. In the real case, there are many variables involved. Is there a simple fix?
This is where functions come in handy. They take in the parameters and return some compiled value.
// create named function
function namedFn($b) {
return "i am a $b";
}
// or anonymous
$f = function ($b) {return "i am a $b"; };
// call function and pass $b as argument
$b = 'boy';
echo namedFn($b);
echo $f($b);
If you really need to reparse some string with the contents of your variables, just use str_ireplace
$a = 'i am a $b';
echo str_ireplace('$b', $b, $a); // $search , $replace , $subject
How could I access specific parameter from a function, example:
function someFunction()
{ echo $a = 7;
echo $b = 70;
}
someFunction();//770
How can I return only $a or $b, is possible ?
echo vs return
First of all, I think it is important to note that echo and return have very different behaviors and are used in very different contexts. echo simply outputs whatever is passed to it, either to an html page, or to the server log.
<?php
$a = 5;
function printFoo() {
echo 'foo';
}
echo '<h1>Hello World!</h1>'; // prints an h1 tag to the page
echo $a; // prints 5 to the page
foo(); // prints foo to the page
?>
return on the other hand is used to "[end] execution of the current function, and return its argument as the value of the function call." return can only ever take one argument. It can also only be executed once in a function; once it is reached, the code will jump out of the function back to where the function was invoked.
<?php
function getFoo() {
return 'foo';
}
// print the value returned by getFoo() directly
echo getFoo();
// store it in a variable to be used elsewhere
$foo = getFoo(); // $foo is now equal to the string 'foo'
function getFooBar() {
return 'foobar'; // code beyond this statement will not be executed
echo 'something something';
return 'another foobar';
}
echo getFooBar(); // prints 'foobar'
?>
function paramters
As it stands, someFunction can only return $a or $b or an array containing $a and $b, which may become a problem if, say, you need to print out $c. To make the function more reusable, you can pass it an argument and then reuse the function wherever you like.
<?php
function printSomething($myVar) {
echo $myVar;
}
$a = 7;
$b = 70;
$c = 770;
printSomething($a) . '\n';
printSomething($b) . '\n';
printSomething($c) . '\n';
printSomething(7000); // you don't have to pass it a variable!
// Output:
// 7
// 70
// 700
// 7000
?>
If you wish to return only one parameter then you just use the return statement.
<?php
function someFunction()
{
$a = 7;
$b = 70;
return [$a, $b];
}
$arrayS = someFunction();//array containing $a and $b
echo $arrayS[0]."\n";
echo $arrayS[1]."\n";
echo "\n";
echo "Another way to access variables\n";
echo someFunction()[0]."\n";
echo someFunction()[1]."\n";
I want to initialise a variable with the contents of another variable, or a predefined value, if said other variable is not set.
I know I could use
if(isset($var1)){
$var2 = $var1;
} else{
$var2 = "predefined value";
}
I thought doing it like this would be more elegant:
$var2 = $var1 || "predefined value";
Last time I checked, this did work (or my memory is fooling me). But here, when I print $var2, it is always 1. It seems PHP checks if the statement $var1 || "predefined value" is true and assigns 1 to $var2.
I want "predefined value" to be assigned to $var2, in case $var1 doesn't exist.
Any peculiarity of PHP I'm missing here to make this work?
Thanks in advance
I generally create a helper function like this:
function issetor(&$var, $def = false) {
return isset($var) ? $var : $def;
}
and call it with the reference of the variable :
$var2 = issetor($var1, "predefined");
I just read that in PHP 7, there will be a new abbreviation for
$c = ($a === NULL) ? $b : $a;
It will look like this:
$c = $a ?? $b;
And can be extended like this:
$c = $a ?? $b ?? $c ?? $d;
explanation: $c will be assigned the first value from the left, which is not NULL.
I'm trying to make a PHP function that wraps around a variable that will check the variable value and change it if it matches another variable.
I'm sure I'm doing it wrong, but...
Here's what I have so far:
<?php
function Clear_Value(){
$val='NONE';
if(this== $val){ this=='';}
};
$one = 'One';
$two = 'Two';
$three = 'NONE';
$four = 'Four';
Clear_Value($one);
Clear_Value($two);
Clear_Value($three);
Clear_Value($four);
echo $one.'<br>';
echo $two.'<br>';
echo $three.'<br>';
echo $four.'<br>';
?>
The output I'm going for would be:
One
Two
Four
I hope that's clear. I'm still learning functions in php so any pointers would be great.
Thanks
You need to pass an argument by reference:
function Clear_Value(&$arg){
if ($arg == 'NONE') $arg = '';
}
This way, the function can modify the variable's contents.
Live example: http://ideone.com/igHc5
what your constant this stands for?
you have to create some input variable in your function and use & prefix (refers to variable in memory)
function Clear_Value(&$var){
$val='NONE';
if($var == $val)
$var = '';
};
and another thing, if you want to change value of a variable, you use just single "=". i recommend you to see basics of syntax at php.net
sorry for my english
I am not sure what you expect here.
But when you execute this program it will show undefined variable error.
Try my sample changed code:-
<?php
function Clear_Value($sam){
$val='NONE';
if($sam== $val){ $sam=='';}
echo $sam.'<br>';
};
$one = 'One';
$two = 'Two';
$three = 'NONE';
$four = 'Four';
Clear_Value($one);
Clear_Value($two);
Clear_Value($three);
Clear_Value($four);
/*
echo $one.'<br>';
echo $two.'<br>';
echo $three.'<br>';
echo $four.'<br>';
*/
?>
Using PHP 5 I would like to know if it is possible for a variable to dynamically reference the value
of multiple variables?
For example
<?php
$var = "hello";
$var2 = " earth";
$var3 = $var.$var2 ;
echo $var3; // hello earth
Now if I change either $var or $var2 I would like $var3 to be updated too.
$var2 =" world";
echo $var3;
This still prints hello earth, but I would like to print "hello world" now :(
Is there any way to achieve this?
No, there is no way to do this in PHP with simple variables. If you wanted to do something like this in PHP, what you'd probably do would be to create a class with member variables for var1 and var2, and then have a method that would give you a calculated value for var3.
This should do the trick. I tested it on PHP 5.3 and it worked. Should also work on any 5.2.x version.
You could easily extend this with an "add"-Method to allow an arbitrary number of strings to be placed in the object.
<?php
class MagicString {
private $references = array();
public function __construct(&$var1, &$var2)
{
$this->references[] = &$var1;
$this->references[] = &$var2;
}
public function __toString()
{
$str = '';
foreach ($this->references as $ref) {
$str .= $ref;
}
return $str;
}
}
$var1 = 'Hello ';
$var2 = 'Earth';
$magic = new MagicString($var1, $var2);
echo "$magic\n"; //puts out 'Hello Earth'
$var2 = 'World';
echo "$magic\n"; //puts out 'Hello World'
No. Cannot be done without utilizing some sort of custom String class.
Check the PHP manual for types and variables, especially this passage:
By default, variables are always
assigned by value. That is to say,
when you assign an expression to a
variable, the entire value of the
original expression is copied into the
destination variable. This means, for
instance, that after assigning one
variable's value to another, changing
one of those variables will have no
effect on the other. For more
information on this kind of
assignment, see the chapter on
Expressions.
it's a bit late, but it's interesting question.
You could do it this way:
$var = "hello";
$var2 = " earth";
$var3 = &$var;
$var4 = &$var2;
echo $var3.$var4; // hello earth
From my point of view the following code is a little closer to the required:
$a = 'a';
$b = 'b';
$c = function() use (&$a, &$b) { return $a.$b; };
echo $c(); // ab
$b = 'c';
echo $c(); // ac
Create a function.
function foobar($1, $2){
$3 = "$1 $2";
return $3;
}
echo foobar("hello", "earth");
echo foobar("goodbye", "jupiter");