-3

I'm writing both the JavaScript and PHP of an app, and bugs like descriptions showing up as "0" due to typos like $desc = $row[0] + " - " + $row[1]; annoy me.

Does PHP have a setting to disable automatic casting of strings to numbers, so i don't notice those typos at the last moment?

Cees Timmerman
  • 17,623
  • 11
  • 91
  • 124

4 Answers4

2

You could sacrifice some performance to gain some consistency and use PHP's sprintf() and sprintf.js

Darsstar
  • 1,885
  • 1
  • 14
  • 14
  • Was thinking before my answer "isn't there a concat function that I have used a lot in php...?". – Jite Nov 20 '13 at 15:06
  • I didn't know about the existence of sprint.js before I googled it. I'm glad it does exist, although it being native to javascript would have been even better. – Darsstar Nov 20 '13 at 15:14
1

+ instead of . is not wrong, its just not what you intended. If you have big issues with this, you could always make your own concat function to use instead, like:

<?php
function concatString(){
   $list = $arglist = func_get_args();
   $ret = "";
   for($i=0;$i<count($arglist);$i++){
     $ret .= $arglist[$i];
   }
   return $ret;
}
echo concatString("Test", "test2", $anotherVar);
Jite
  • 5,761
  • 2
  • 23
  • 37
0

use $desc = $row[0] . " - " . $row[1]; if you want to get a

avrgavr
  • 1
  • 1
  • That's what i should have typed. Instead i used the JavaScript concatenation syntax, which is a typist mistake hence a "typo". – Cees Timmerman Nov 20 '13 at 14:55
0

No; PHP loves to autoconvert strings:

<?php
$foo = 1 + "10.5";                // $foo is float (11.5)
$foo = 1 + "-1.3e3";              // $foo is float (-1299)
$foo = 1 + "bob-1.3e3";           // $foo is integer (1)
$foo = 1 + "bob3";                // $foo is integer (1)
$foo = 1 + "10 Small Pigs";       // $foo is integer (11)
$foo = 4 + "10.2 Little Piggies"; // $foo is float (14.2)
$foo = "10.0 pigs " + 1;          // $foo is float (11)
$foo = "10.0 pigs " + 1.0;        // $foo is float (11)     
?>

As commented, manual code scanning can detect ridiculous cases like that, and could be automated by hooking saves. PHPLint on-line is free and easy to use:

PHPLint report

PHPLint 1.1_20130803
Copyright 2013 by icosaedro.it di Umberto Salsi
This is free software; see the license for copying conditions.
More info: http://www.icosaedro.it/phplint/


BEGIN parsing of test-p4Gx1L
1:      <?php
2:      $a = "Hi ";
3:      $b = "Bob";
4:      $c = $a + $b;

        $c = $a + $b;
                     \_ HERE
==== 4: ERROR: `EXPR + ...': expected number or array but found string

        $c = $a + $b;
                     \_ HERE
==== 4: ERROR: `... + EXPR': expected number or array but found string
5:      
6:      echo $c;
7:      ?>
END parsing of test-p4Gx1L
==== ?: notice: unused package `stdlib/dummy.php'
==== ?: notice: unused module `mysql'
==== ?: notice: unused module `pcre'
==== ?: notice: required module `standard'
Overall test results: 2 errors, 0 warnings.

Talking to MySQL can still produce unexpected results, though.

Community
  • 1
  • 1
Cees Timmerman
  • 17,623
  • 11
  • 91
  • 124