concate php variable - php

When concatenate php variable result not showing.
$a = 5;
$b = 4;
$o = '+';
echo $a.$o.$b;
result showing 5+4; but i want show result 9
How can i do this, anybody can help me out.
Thanks in advance.

'+' is a string, so if you concate it with a number you get a string. You have to look into the value:
if ($o == '+') {
echo $a + $b;
}
Or what you probably want:
switch ($o) {
case "+":
echo $a + $b;
break;
case "-":
echo $a - $b;
break;
case "*":
echo $a * $b;
break;
case "/":
echo $a / $b;
break;
default:
echo 0;
}

Use the "eval()" function
As in:--
echo eval($a.$o.$b)
But be careful never "eval" anything that comes from a web page without validation.

You are concatenating with the string "+", so you get a string. You want to actually add the numbers:
$o = $a + $b;
echo $o;

concatenate joins strings. If you want normal arithmetic, just use a plus sign:
$a = 5;
$b = 4;
echo ($a + $b)

When you use concatenate PHP automatically set type of parameters STRING. You should write $c=$b+$a; echo $c;

You can do this:
function calc($a, $b, $o) {
$op = array('+', '-');
if (in_array($o, $op))
return eval('return '.(int)$a.$o.(int)$b.';');
return False;
}
$a = 5;
$b = 4;
$o = '+';
var_dump(calc($a, $b, $o));
But this is very ugly, you should rethink your logic.

Related

Advanced php formatting need help understand layout

Here is the function:
function simplePresent($e) {
$w = ($f = preg_match)('/ey|se|d |[sI]$|We/', $a = $e[0]);
be == ($b = $e[1])
?
$b = $w ? $a == I ? am : are : is
:
$w ?: $b = $b == have ? has : $b .= $f('/[h-z]$/', $b) ? es : s;
return "$a $b $e[2]";
}
this is a solution on codefights. it handles formatting under defined parameters. I am trying to understand the function assignment to the variables $f and $w. Also the the use of variables with no quotes. lastly the nested question marks and colons. How is this functioning?
Thanks
The function preg_match() is being assigned to the variable $f and reused later in the code. $w is simply the result of preg_match(). For example:
<?php
$w = ($f = 'sprintf')("foo");
// same as this:
$f = 'sprintf';
$w = $f("foo");
// which is the same as this:
$w = sprintf('foo');
The unquoted values will first be interpreted as constants, and the undefined constants will be interpreted as strings. Here's how a proper constant definition looks:
<?php
define("foo", "bar");
echo foo;
echo bar;
// PHP Notice: Use of undefined constant bar - assumed 'bar'
// same as this:
echo "bar";
echo "bar";
The question marks and colons are part of a ternary statement. This is a useful shorthand, but PHP recommends against stacking them together as is done there (the result is "non-obvious.") A simple one looks like this:
<?php
echo ($foo == "foo" ? "equal" : "inequal");
$a = $b ? $c : $d;
// this is the same as:
if ($foo == "foo") {
echo "equal";
} else {
echo "inequal";
}
if ($b) {
$a = $c;
} else {
$a = $d;
}

number_format multiple vars at once in php

I have the following script
$a = 434343434343;
$b = $a *3;
$c = $a * 6;
print $a;
print $b;
print $c;
I want all three variables to be returned using the number_format($var) syntax. The three vars are being printed in various parts of an html template. What is the best way to do this for all three vars at once? Should I add these vars to an array and number_format the array?
The best that I can come up with is the following:
$a = 434343434343;
$b = $a *3;
$c = $a * 6;
$a = number_format($a);
$b = number_format($b);
$c = number_format($c);
print $a;
print $b;
print $c;
Is that preferred?
Put those numbers inside an array and format the array, it's faster.
$numbers = array();
$numbers['a'] = 434343434343;
$numbers['b'] = $numbers['a'] * 3;
$numbers['c'] = $numbers['a'] * 6;
foreach($numbers as $key => $val)
{
$numbers[$key] = number_format($val);
}
by the way, if you NEED the values as variables, you can extract them:
extract($numbers); //creates the variables $a, $b, $c
echo $a;
echo $b;
echo $c;
You can see it in action right here.
Seems I found a much better solution.
$a = number_format(434343434343);
$b = number_format($a *3);
$c = number_format($a * 6);
//$a = number_format($a);
//$b = number_format($b);
//$c = number_format($c);
//output
print $a;
print $b;
print $c;

if a variable is referenced inside a function then what happens

function a(&$c, &$d){
$c = &$d;
}
$a = 1;
$b = 2;
a($a, $b);
echo $a;
output is 1,but shouldn't it be outputting 2 as $c is referencing $d. $c and $a reference to the same value,then $c refer to the value of $d which refer to $b so ultimately $a should refer to $b, isn't it correct?
For the operation you seek you must remove the ampersand reference used within the function a.
Example
function swap (&$one, &$two) {
$tmp = $one; // One is stored temporarily
$one = $two; // Two is stored in One
$two = $tmp; // Temporary data retrieved and stored in two
unset($tmp); // Temporary variable destroyed
}
// Set the variables
$a = 1;
$b = 2;
echo $a . " - " . $b . "<br />"; // See output as: 1 - 2
swap($a, $b);
echo $a . " - " . $b; // See output as: 2 - 1
in line
$c = &$d;
$a is referring to the address of $b but not to $b, then you can try this:
<?php
function foo(&$c, &$d){
$c = $d;
}
$a = 1;
$b = 2;
foo($a, $b);
echo $a;
?>

Insert variable into string at random position

See this code:
<?php
$a = rand(1, 10000000000);
$b = "abcdefghi";
?>
How can I insert $b into a random position of $a?
Assuming "casual" means random:
<?php
$a = rand(1, 10000000000);
$b = "abcdefghi";
//get a random position in a
$randPos = rand(0,strlen($a));
//insert $b in $a
$c = substr($a, 0, $randPos).$b.substr($a, $randPos);
var_dump($c);
?>
above code working: http://codepad.org/VCNBAYt1
Edit: had the vars backwards. I read "insert a into b,
I guess you could by treating $a as a string and concatenating it with $b:
$a = rand(1, 1000000);
$b= "abcd";
$pos = rand(0, strlen($a));
$a = substr($a, 0, $pos).$b.substr($a, $pos, strlen($a)-$pos);
and the results:
a=525019
pos=4
a=5250abcd19
a=128715
pos=5
a=12871abcd5
You should put {$b} on top of {$a} so that you can insert it to {$b}..
eg:
<?php
$b = "abcdefghi";
$a = rand(1, 10000000000);
$a .= $b;
echo $a;
?>
Sth like this :
<?php
$position = GetRandomPosition(); // you will have to implement this function
if($position >= strlen($a) - 1) {
$a .= $b;
} else {
$str = str_split($a, $position);
$a = $str[0] . $b . implode(array_diff($str, array($str[0])));
}
?>
Cast $a to string, then use strlen to get the length of $a. Use rand, with with the length of $a as the maximum, to get a random position within $a. Then use substr_replace to insert $b into $a at the position you've just randomized.

PHP use string as operator

Say I have a string, $char. $char == "*".
I also have two variables, $a and $b, which equal "4" and "5" respectively.
How do I get the result of $a $char $b, ie 4 * 5 ?
Thanks :)
You can use eval() as suggested by #konforce, however the safest route would be something like:
$left = (int)$a;
$right = (int)$b;
$result = 0;
switch($char){
case "*":
$result = $left * $right;
break;
case "+";
$result = $left + $right;
break;
// etc
}
safest method is a switch construct:
function my_operator($a, $b, $char) {
switch($char) {
case '=': return $a = $b;
case '*': return $a * $b;
case '+': return $a + $b;
etc...
}
}
The easiest but most dangerous method is to use eval.
$c = eval("return $a $char $b;");
take a look at the eval() function. you will need to build a proper php command and run inside the eval() to extract out the result.
You can do with eval however I would not suggest using eval.
If there is case operator can by anything you should check what operator is before using
switch($char)
{
case '*':
$result= $a * $b;
break;
case '+':
$result= $a + $b;
break;
}
<?php
$a = 'alex';
$b = "alex";
$c = "==";
function abc($a,$b,$c){
$d = 'return ($a '.$c.' $b) ? true : false;';
return eval($d);
}
if(abc($a,$b,$c)){
echo "condition true";
}else{
echo "condition false";
}
// echo $e;
?>

Categories