Why Does "Echo '2' . Print(2) + 3" Print 521

Why does echo '2' . print(2) + 3 print 521?

Echo a concatenated string composed of:

The string '2'
The result of the function print('2'), which will return true, which gets stringified to 1
The string '3'

Now, the order of operations is really funny here, that can't end up with 521 at all! Let's try a variant to figure out what's going wrong.

echo '2'.print(2) + 3;
This yields 521

PHP is parsing that, then, as:

echo '2' . (print('2') + '3'))
Bingo! The print on the left get evaluated first, printing '5', which leaves us

echo '1' . print('2')
Then the left print gets evaluated, so we've now printed '52', leaving us with

echo '1' . '1' ;
Success. 521.

I would highly suggest not echoing the result of a print, nor printing the results of an echo. Doing so is highly nonsensical to begin with.

echo with print - unexpected answer

As executed, the code will do:

print '2'  -> outputs 2
... print ALWAYS has a return value of 1, so the code becomes

echo '1' . (1 + 3); // with output '2'

This is simplified to

echo '1' . 4; // with output '2'
echo '14'; // output 2

final output: 214.

Explanation for PHP print adding weirdness

Print is not a function, so print(99) is the same as print 99. We can remove the parentheses for clarity.

print 99 + print 99 + print 99 + print 99;

The expression is evaluated from the right, so it becomes

print (99 + print (99 + print (99 + print 99)));

The rightmost print executes first, printing "99" and evaluating to 1.

Output:

99

Code left to be evaluated:

print (99 + print (99 + print (99 + 1)));

Again, the rightmost print is executed and it prints 99+1 ("100") and evaluates to 1.

Output:

99100

Code left to be evaluated:

print (99 + print (99 + 1));

...and so on.

Echo results on same line?

Try something like this:

<?php

$sale_price = $row->price - (($row->price /100) * $row->discount);

echo "<h3><strike>" . number_format($row->price,2) . " USD </strike> " . number_format($sale_price,2) . " USD</h3>";

?>

or this perhaps:

$<?php $sale_price = $row->price - (($row->price /100) * $row->discount);?>

<h3><strike>$<?php echo number_format($row->price,2);?> USD </strike>$<?php echo number_format($sale_price,2);?> USD</h3>

The problem is mostly in the place where you use the <h3> tags, and because you have put the original price outside of that tag.

Printing 1 to 1000 without loop or conditionals - in PHP

Here's an interesting oo solution based on PHP's overloading:

class thousand_printer {
public function __construct() {
$this->print1();
}

public function __call($method, $_) {
$count = str_replace('print', '', $method);
echo "$count ";
$this->{"print" . ++$count}();
}

public function print1000() {
echo "1000\n";
}
}

new thousand_printer;

I'm glad my solution is so popular. Here's a slight improvement that offers some modularity:

class printer {
public function __construct() {
$this->print1();
}
public function __call($method, $_) {
$count = str_replace('print', '', $method);
echo "$count ";
$this->{"print" . ++$count}();
}
}

class thousand_printer extends printer {
public function print1001() {}
}

new thousand_printer;

Is there a pretty print for PHP?

Both print_r() and var_dump() will output visual representations of objects within PHP.

$arr = array('one' => 1);
print_r($arr);
var_dump($arr);

Awk include any of the following into one print statemrnt

to count the number of lines for the given condition

awk '$9=="421" || $9=="221" || $9=="511" {count++}
END {print count}' file

you can also write it more compactly

awk '$9 ~ /^(421|221|511)$/{c++} END{print c}' file

when I try this on your sample input

$ awk '$9 ~ /^(421|221|511)$/{c++} END{print c}' file
5

to check what lines matched

$ awk '$9 ~ /^(421|221|511)$/' file

4/13/2016 5:00:00 192.168.33.118 - GCO\johnsmith [1190]pass ******* - 221 - - - 9342
4/13/2016 5:00:00 192.168.33.118 - GCO\johnsmith [1190]quit - - 421 - - - 9342
4/13/2016 5:00:00 192.168.33.118 - - [1190]ssh_disconnect disconnect_by_application - 511 - - - 9342
4/13/2016 5:00:00 192.168.33.118 - GCO\johnsmith [1178]dele /P0016/pCR37558690_0.pdf - 511 - - - 9341
4/13/2016 5:00:00 192.168.33.118 - GCO\johnsmith [1175]user GCO\johnsmith - 221 - - - 9340

print 1 to 100 in php using recursive function without parameter and condition

The pragmatic answer, at least, is "no." A recursive function must have an exit condition which causes the recursion to stop. It must test for that condition.

Anything that tries not to do that is "code golf," including Rax's answer above which merely conceals the existence of the necessary conditional tests. (In that case, it's buried in str_repeat(), which contains a loop that stops after 100 iterations.)

AWK-Argument list too long (For good reason)

First create a map file like: first column is the min value, the second column is max, the third column is the output. Let's name the file mapfile.txt:

 363  499 0
4645 4646 0
2174 2193 1
...

Then run awk such as (untested, typos expected):

awk 'FNR == NR { ++i; min[i]=$1; max[i]=$2; result[i]=$3; }
FNR != NR {
for (j = 1; j <= i; ++j) {
if (min[j] <= $1 && $1 <= max[j]) {
print result[j];
break
}
}
}
' mapfile.txt second_column_values.txt

First we read the map file into memory and three arrays. Then we check the value for min/max and print the result if found. Then if the result is found - we break from the loop.

Alternatively, if you have work with huge files, you could do this:

  1. First create another mapfile such the first column is the value and the second is the result. It could be generated from the mapfile.txt above with something like while read a b c; do seq -f "%.0f $c" $a $b; done < mapfile.txt

363 0
364 0
365 0
...

  1. Remember to sort this file. Let's call it mapfile2.txt

  2. Then having numbers from column 2, add a line number on each line, sort it on the second column, then join it with the mapfile2.txt, re-sort on line numbers and remove line numbers.

nl -w1 second_column_values.txt | sort -s -k2 |
join -12 -21 - <(<mapfile2.txt sort -s -k1) |
sort -s -k1 | cut -f2-

Or do the same without numbering the lines if the order of lines does not matter. I think sort+join-ing the files could be faster then plain array lookup with range comparison in very extreme cases.



Related Topics



Leave a reply



Submit