Advanced php formatting need help understand layout - php

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;
}

Related

PHP use result of as variable

Is it possible to use the result of an if with an OR statement as a variable for a function?
As example:
$a = true;
$b = false;
if ($a || $b) {
$this->functionCall($a)
}
Other example:
$a = false;
$b = true;
if ($a || $b) {
$this->functionCall($b)
}
Third and final exmaple:
$a = true;
$b = true;
if ($a || $b) {
$this->functionCall($a, $b)
}
So I need to detect what variable is true and pass it as a paramater. Is this even possible?
Any helps is appreciated!
Many thanks in advance
I'd do the logic bit inside a two-parameter function if I were you, as such :
function myFunc($a = false, $b = false) {
if ($a == true)
echo 'a';
if ($b == true)
echo 'b';
}
myFunc(); // echoes nothing
$a = true;
$b = false;
myFunc($a, $b); // echoes 'a'
$a = false;
$b = true;
myFunc($a, $b); // echoes 'b'
$a = true;
$b = true;
myFunc($a, $b); // echoes 'ab'
PHP 5.6+ version, filter out the falsely values (you can pass a callback to array_filter for different checks) and use those with the splat operator.
$params = array_filter([$a, $b]);
$this->callFunction(...$params);
No need for any IF checks and confusing in IF assignments.
Explore Variadic functions and Argument unpacking.

php could not use $$ with array

I know that
$b = 1;
$var = "b";
$$var = 2;
echo $b;
will show 2
But when I try it on array, it fail
$c[1] = 1;
$var = "c[1]";
$$var = 2;
echo $c[1];
$d[1] = 1;
$var = "d";
$$var[1] = 2;
echo $d[1];
they both show 1, why?
In your first example, you can't use the index because it is assumed part of the variable name.
In the second, you need to use the curly braces for a complex syntax to disambiguate. This way PHP knows that it's the contents of $d[1] and not the contents of $var[1].
$d[1] = 1;
$var = "d";
${$var}[1] = 2;
echo $d[1];

Do you know a nice PHP hack to do inline conditional assignation?

I really enjoy || operator in JavaScript, where we can do inline conditional assignation.
var a = 0;
var b = 42;
var test = a || b || 'default value';
console.log(test); // 42
This is clear to read, and don't take too many lines.
In PHP, this logical operator return booleans:
$a = 0;
$b = 42;
$test = $a || $b || 'default value';
print_r($test); // bool(true)
Of course, we can do inline assignation using ternaries:
$test = $a ? $a : $b ? $b : 'default';
print_r($test); // int(42)
But this make code ambiguous, this is not that easy to read.
So here my question come:
Do you know a nice PHP hack to do inline conditional assignation ?
In PHP 5.3+ you can do this:
$test = $a ?: ($b ?: 'default value');
This will work as long as you don't need to short-circuit side effects:
function either_or() {
$nargs = func_num_args();
if ($nargs == 0) {
return false;
}
$args = func_get_args();
for ($i = 0; $i < $nargs-1; $i++) {
if ($args[$i]) {
return $args[$i];
}
}
return $args[$nargs-1];
}
$test = either_or($a, $b, "Default value");

Make a substitution between the values of two variables in PHP

I was wondering if it could be possible to make a substitution between the values of two variables, in PHP.
I can explain it better:
<?php
$a = "Cat";
$b = "Dog";
// The strange/non-existent function I am talking about //
MakeSubstitution($a, $b);
// After this (non-existent) function the values of the variables should be:
// $a = "Dog"
// $b = "Cat"
?>
So, does it exist? I made searches but I found no results.
Thanks in advance.
Try this :
$a = "Cat";
$b = "Dog";
list($a,$b) = array($b,$a);
echo $a;
echo $b;
Handle them by reference in a function, and swap their values:
function swap ( &$a, &$b ) {
$t = $a; // Create temp variable with value of $a
$a = $b; // Assign to $a value of $b
$b = $t; // Assign to $b value of temp variable
}
$dog = "dog";
$cat = "cat";
swap($dog, $cat);
echo $dog; // Output 'cat'
Apparently you can use a bitwise operator too, and avoid the overhead of creating a temporary function/var/array:
$cat = "cat";
$dog = "dog";
$cat = $cat ^ $dog;
$dog = $cat ^ $dog;
$cat = $cat ^ $dog;
echo $cat . $dog; // Output 'dogcat'
Managed to find a great illustration of the bitwise approach: https://stackoverflow.com/a/528946/54680

concate php variable

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.

Categories