Asked  7 Months ago    Answers:  5   Viewed   40 times

Given this example, how would I return the result of the equation rather than the equation itself as a string?

$operator = '+';
foreach($resultSet as $item){
    $result = $item[$this->orderField] . $operator . 1;
    echo $result;



You could make functions that wrap the operators, or for simplicity just use the bc extension:

$operator = '+';
$operators = array(
  '+' => 'bcadd',
  '-' => 'bcsub',
  '*' => 'bcmul',
  '/' => 'bcdiv'

foreach($resultSet as $item){
    $result = call_user_func($operators[$operator], $item[$this->orderField], 1);
    echo $result;
Wednesday, March 31, 2021
answered 7 Months ago

I think you are looking for PHP's session handling stuff ...

As an example, first page:

session_start(); # start session handling.
$_SESSION['test']='hello world';

second page:

session_start(); # start session handling again.
echo $_SESSION['test']; # prints out 'hello world'

Behind the scenes, php has set a cookie in the users browser when you first call session start, serialized the $_SESSION array to disk at the end of execution, and then when it receives the cookie back on the next page request, it matches the serialised data and loads it back as the $_SESSION array when you call session_start();

Full details on the session handling stuff:

Wednesday, March 31, 2021
answered 7 Months ago

You can see examples about this behaviour in Logical Operators

Also you can read artical about Short-circuit evaluation

The short-circuit expression x Sand y (using Sand to denote the short-circuit variety) is equivalent to the conditional expression if x then y else false; the expression x Sor y is equivalent to if x then true else y.

In php.

return x() and y();

equal to

if (x())
  return (bool)y();
  return false;

return x() or y();

equal to

if (x())
  return true;
  return (bool)y();

So, deal is not just in precedence.

Wednesday, March 31, 2021
answered 7 Months ago

Turns out you can!

First declare the defaults like this:

define('WEB_ROOT', '/var/www', TRUE);

Then include the config file:

define('WEB_ROOT', '/home/user/public_html');

This code works well:

define('FOO', 'BAR', TRUE);
echo FOO; // outputs BAR
define('FOO', 'FOOBAR');
echo FOO; // outputs FOOBAR

This is a side effect of the incredible fact that PHP supports declaring case insensitive constants.

Saturday, May 29, 2021
answered 5 Months ago

== means equality, so the conditional reads as:

If pre-incremented $x equals 10, echo $x

Single = is assignment, where a variable is set to contain a value:

$word = 'hello';
$number = 5;
// etc.

echo "I said $word $number times!";

Regarding the increment opperators:

You'll see things like ++$x and $i-- as you learn PHP (and/or other languages). These are increment/decrement operators. Where they're positioned in relation to the variable they're operating on is important.

If they're placed before the variable, like ++$x, it's a pre-increment/decrement. This means the operation is performed before anything else can be done to the variable. If it's placed after, like $x++, it's a post-increment/decrement, and it means that the operation is performed afterward.

It's easiest to see in an example script:

$x = 5;

echo ++$x; // 6
echo $x++; // ALSO 6
echo $x; // NOW 7
Saturday, August 7, 2021
answered 3 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :