Don't think this exists but just want to make sure.
Is there a reverse for the .= operator in php.
example:
[$x .= $y] === [$x = $x.$y]
looking for:
[$x ? $y] === [$x = $y.$x]
No, there isn't.
The PHP manual documentation only lists two operators:
There are two string operators. The first is the concatenation operator ('.'), which returns the concatenation of its right and left arguments. The second is the concatenating assignment operator ('.='), which appends the argument on the right side to the argument on the left side.
When you want to prepend to a string, just do $str = $add . $str. There is no need for a "special operator" here. If you use it frequently and don't want to retype it every time, you can create a function like this:
function prepend($text, $add) {
return $add . $text;
}
But, as you can probably guess, it's pointess.
Amal Murali's answer gave me an idea.
function p(&$x,$y){
$x = $y.$x;
return $x;
}
Now instead of having to type out;
$var1 = $var2 . $var1;
You can just do:
p($var1, $var2);
If you send the first variable as reference it does clean the code a bit.
Variables:
$var = ' world';
$var2 = 'Hello';
Usage
echo p($var, $var2);
or
p($var, $var2);
echo $var;
is now equal to:
$var = $var2 . $var1;
echo $var;
Related
This is not the first time I encountered the problem. I have 2 variables in PHP of type string. When both of them are not empty I want something like this :
echo $var1 . ', ' . $var2;
My problem is that sometimes $var1 or $var2 could be empty so the coma is floating. I try this :
echo $var1 && $var2 ? $var1 . ', ' . $var2 : $var1 || $var2;
But when we are in the right condition, it send 1. I hope the only way is not to test $var1 && $var2 then test $var1 and then test $var2.
Thanks in advance,
echo join(', ', array_filter([$var1, $var2]));
array_filter removes all empty values, and join puts the joiner between the remaining items, if there are more than one.
You can do it by many way e.g by checking empty, using ternary and so on but I've one way to fix this using array_filter() to remove empty values and implode with , . Then it'll not floating when any of the variable is empty string and it can consume multiple variables. Hope this helps :)
<?php
$implode = ', ';
$var1 = 'xyz';
$var2 = '';
$result = [$var1,$var2];
$filtered_result = array_filter($result);
echo implode($implode,$filtered_result);
?>
DEMO: https://3v4l.org/PZ8Rv
try this
if(!(empty($var1) && empty($var2))){
echo $var1 . ', '.$var2;
}else{
$value = !empty($var1)? $var1 : (!empty($var2)? $var2 : "both are empty");
echo $value
}
your right condition use || operator which returns a boolean. if the right statement or left statement is true it will return true (1), or it will return false (0)
then you have to do something like this
echo ($var1 && $var2 ? $var1 . ', ' . $var2 : ($var1 ? $var1 : $var2));
note that $var1 has to be defined (null or empty but it has to be defined)
I have 3 variables :
$a = 5;
$b = 3
$o = "*";
The $o variable contains a string value. This value can also be "/", "+", "-".
So when I concatenate $a.$o.$b :
$result = $a.$o.$b;
echo $result;
The result is 5*3 (a string) instead of 15.
So how to convert operator string into real operator ?
You can't, you'd need to make a function or a switch statement to check each of the operators.
Read this question and its answers so you'll understand how to do it.
Actually you can do it, by using eval. But it hurts to recommend using eval, so I just give you a link to another question with a good answer: calculate math expression from a string using eval
You can use eval. For example:
eval('echo 5*3;')
will echo the number 15.
Simple, short and save solution:
$a = 5;
$b = 3
$o = "*";
$simplecalc = function($a,$b,$op) {
switch($op):
case "*":
return $a*$b;
case "+":
return $a+$b;
case "-":
return $a-$b;
case "/";
return $a/$b;
default:
return false;
endswitch;
};
$result = $simplecalc($a,$b,$o);
I believe this is what you are looking for.
$a = 5;
$b = 3;
$o = "*";
$result = eval( "echo $a$o$b;" );
echo $result;
Please note that using eval() is very dangerous, because it allows execution of arbitrary PHP code.
I'm wondering if it is possible to update a variable which is inside another variable.
Here is an example:
$t = 15;
$dir ='foo and some more text'.$t.'and more foo';
$t = 10;
print_r($dir);
For me $dir outputs $t as 15 not as 10.
Can anyone help me with this?
You're misunderstanding what that code is actually doing. This line:
$dir ='foo and some more text'.$t.'and more foo';
doesn't store a reference to $t for future evaluation. It evaluates $t to whatever value it has at that time and uses the result to construct the value placed in $dir. Any reference to $t is lost before the engine even gets to the step of assigning it to $dir.
You can pass a variable to a function, you can encapsulate variable state in an object, but an evaluated string doesn't reference a variable.
This is not possible. But you can make something similar with preg_match and a custom print function.
This is an just example how it could be done (warning: experimental):
<?php
$blub = 15;
$test = 'foo and some more text %blub and more foo %%a';
function printv($text) {
$parsedText = preg_replace_callback('~%([%A-Za-z0-9]+)~i', function($matches) {
if ($matches[1][0] != '%') {
return $GLOBALS[$matches[1]];
}
return $matches[1];
}, $text);
echo $parsedText;
}
$blub = 17;
printv($test);
?>
What ever be the value of $t at the time of assigning $dir the value is 15. That will be stored and assigned. This is same for all the languages.
Or if you want to, its easy to do it with anonymous function.
$dir = function ($t) {return 'foo and some more text'.$t.'and more foo';}
echo $dir(10);
//foo and some more text10and more foo
echo $dir(15);
//foo and some more text15and more foo
I am trying to insert the following if statement into a class but getting the unexpected T_IF error;
class="color_icon_filter '. if ($link_data['id']==$link_data['text']) {$active} .' "
is this not possible or am I doing it wrong?
You shoud open PHP tags to be interpreted server side.
class="color_icon_filter <?php if ($link_data['id']==$link_data['text']) {echo $active} ?>"
EDIT
Or if you're already in PHP tags
echo 'class="color_icon_filter '.($link_data['id']==$link_data['text'] ? $active : '').'"';
It is not possible to concatenate an if statement onto a string.
You can do this instead:
$string = "123";
if ($bar) {
$string .= "456";
}
$string .= "789";
or this:
if ($bar) {
$baz = "456";
} else {
$baz = "";
}
$string = "123" . $baz . "789";
You could also use a ternary operator, but your if condition is (relatively) long, so it risks making your code look even more like line noise.
You're trying to concat in an if statement.
I'm assuming there's an echo in front of that line. You can either convert that to sprintf (http://php.net/sprintf) or put the if before the echo line, set the value of $active, and then concat in just the variable.
Personally, I think sprintf/printf is your better solution.
You cannot use an if statement in the middle of an assignment operation, but there are ways to achieve your desired result:
$active = '';
if ( $link_data['id']==$link_data['text'] ) {
$active = 'active';
}
$class = "color_icon_filter $active";
or a shorter way via the ternary operator:
$active = ( $link_data['id']==$link_data['text'] ) ? 'active' : '';
$class = "color_icon_filter $active";
I know it is possible in php to concatenate two strings like
$a .= $b;
which is equal to
$a = $a . $b;
Is it possible to do something with integers? but with math operations? I have two variables:
$var1 = 8;
$var2 = 2;
I need to do $var1 - $var2, but I don't want to create third variable to hold this calculation. I would try $var1 = $var1 + $var2 will this work? Is there another .= similar way of doing it?
OK, because everybody is confused now
// _Appending_ $var2 to $var1 and assign it to $var1
$var1 .= $var2;
// _Add_ $var2 to $var1 and assign the result to $var1
$var1 += $var2;
There is nothing more to remember than, that every binary operator is combineable with the assignment. Within the engine it's always expanded to
$var1 = $var1 . $var2; // You can imagine how the others looks like
In addition to the basic assignment operator, there are "combined operators" for all of the binary arithmetic, array union and string operators that allow you to use a value in an expression and then set its value to the result of that expression.
I think you need a string formatting.
$var1 = sprintf( "%d%d", $var1, $var2 );
which will make your $var1 as 82.
As in C and C++, PHP supports Compound_assignment_operators:
So, the line:
$var1 = $var1 + $var2;
is equivalent to:
$var1 += $var2;
For full list of those operators, take a look at Compound_assignment_operators
Yes
$var1 = 1
$var2 = -1;
$var1 += $var2; // $var1 = 0
Yes, Compound Assignment operators will work in php as C & Java.
you can try like this
$Var1+=$Var2;
$var1=$var1-$var2;
will work fine, but shorthand operators like
$var1=-$var2
will not work.
I will set -2 to $var1
Notation for shorthand operator is :
$var1-=$var2;