Problems prints a string that has math operations in the middle

If you try to run math operations inside of a string, you must encapsulate them in parentheses otherwise you get unexpected output.

The first example is just printing the number of entries in the $a_files array. The second is performing a math operation which has the strange output, and the third is the correct way to do this operation.

<?php
$a_files = array("file1","file2","file3","file4");

### Outputs: This array contains 4 files.
print("This array contains " . count($a_files) . " files.\n");

### Outputs: -2 files.
print("This array contains " . count($a_files)-2 . " files.\n");

### Outputs: This array contains 2 files.
print("This array contains " . (count($a_files)-2) . " files.\n");
?>

Page Comments (Click to edit)






[Click to add or edit comments])

Please prepend comments below including a date

Design by N.Design Studio, adapted by solidGone.org (version 1.0.0)
Have a nice day.