Reference - What Does This Error Mean in PHP

PHP: What does &$this mean?

I suppose the code came from PHP4 time.

re #1 - In PHP 4 using array($this) means the same as array(clone($this)) in PHP 5. that is you get a copy of an object instead. In most cases you would like to get the same object though, not its copy. So I suppose the original author used array(&$this) exactly for that reason.

re #2 is explained in Returning References chapter of the manual

in this case it it doesn't matter if it's &$this or some &$obj. The reference in fact is applied to the value returned from foo(). This might make sense for example if foo() has some big static $cache object inside and returns part of it back. E.g.

class MyClass {
function &foo($id){
static $cache;
if (!isset($cache[$id])) {
$cache[$id] = extract_some_object_from_database($id);
}
return $cache[$id];
}
}

Notice the "&foo"

Reference — What does this symbol mean in PHP?

Incrementing / Decrementing Operators

++ increment operator

-- decrement operator

Example    Name              Effect
---------------------------------------------------------------------
++$a Pre-increment Increments $a by one, then returns $a.
$a++ Post-increment Returns $a, then increments $a by one.
--$a Pre-decrement Decrements $a by one, then returns $a.
$a-- Post-decrement Returns $a, then decrements $a by one.

These can go before or after the variable.

If put before the variable, the increment/decrement operation is done to the variable first then the result is returned. If put after the variable, the variable is first returned, then the increment/decrement operation is done.

For example:

$apples = 10;
for ($i = 0; $i < 10; ++$i) {
echo 'I have ' . $apples-- . " apples. I just ate one.\n";
}

Live example

In the case above ++$i is used, since it is faster. $i++ would have the same results.

Pre-increment is a little bit faster because it really increments the variable and after that 'returns' the result. Post-increment creates a special variable, copies there the value of the first variable and only after the first variable is used, replaces its value with second's.

However, you must use $apples--, since first, you want to display the current number of apples, and then you want to subtract one from it.

You can also increment letters in PHP:

$i = "a";
while ($i < "c") {
echo $i++;
}

Once z is reached aa is next, and so on.

Note that character variables can be incremented but not decremented and even so only plain ASCII characters (a-z and A-Z) are supported.


Stack Overflow Posts:

  • Understanding Incrementing

PHP: How do I fix this type hinting error?

From the manual

Typed pass-by-reference Parameters

Declared types of reference parameters are checked on function entry, but not when the function returns, so after the function had returned, the argument's type may have changed.

Given you are immediately assigning a new value to $output, its type declaration is irrelevant. Either omit the type declaration or mark it nullable

function foo(int $one, int $two, ?int &$output): void
{
$output = $one + $two;
}

https://3v4l.org/j91DG


Of course, this type of pattern is convoluted and makes no sense over something as simple as

function foo(int $one, int $two): int
{
return $one + $two;
}

php Only variables should be passed by reference in error

The question itself is already answered by El_Vanja in the comments, but given you're beginner, I'd suggest to separate the evaluation of your variables from returning the outcome of that evaluation. That way, your code would become more readable, thus more maintainable, and easier to refactor - leaving you with far more room to improve both your code and your coding abilities.

The ternary operator, for example, is best suited for one-liners, while in your case you might resort to plain ol' if clauses. It's a pretty verbose example, but you could approach evaluating your variables more along the lines of:


function _is_compiled(&$template_path, &$compile_path) {

if (!is_file($compile_path)) {

return false;
}

$template = file($compile_path);
$matches = preg_match(

'#^<\?php /\* Template_ 1.0.0 (\d+) ([^*]+) \*/#',
array_shift($template),
$match
);

if (!$matches) {

return false;
}

if ($match[1] != filemtime($template_path)) {

return false;
}

if ($match[2] != $template_path) {

return false;
}

$USET = new UserSet;
$tmp = $USET->readSet('sttime');

if ($tmp['setuptime'] > filemtime($template_path))) {

return false;
}

return true;
}

How brief or verbose you should be is a discussion as old as coding I suppose, but one interesting take on the topic is this one:

  • https://jshakespeare.com/code-isnt-poetry/

Found parse error in php

PHP variables always start with a $ sign, so whenever you use a variable the $ sign is mandatory.

The corrected code looks like:

<?php
$counter = 0;
while ($counter < 1) {

switch($_POST['FirstNum'])
{
case "1":
echo "Addition";
$counter++; break;
case "2":
echo "Subtraction";
$counter++; break;
case "3":
echo "Multiplication";
$counter++; break;
case "4":
echo "Division";
$counter++; break;
default:
echo "Invalid Operation";
}
}
?>


Related Topics



Leave a reply



Submit