Zen Cart Logo
Forums / Code Collaboration / PHP 'mixed' comparisons ... a cautionary tale.

PHP 'mixed' comparisons ... a cautionary tale.

Views: 5,540

Results 1 to 1 of 1
4 Nov 2019, 5:05 PM
#1
lat9 avatar

lat9

Administrator

Join Date:
Sep 2009
Location:
Stuart, FL
Posts:
14,065
Plugin Contributions:
56

PHP 'mixed' comparisons ... a cautionary tale.

Be careful when comparing variables of different types, e.g. string vs. integer. Here's a teeny example to illustrate the potential issue:

<?php
require 'includes/application_top.php';

$uprid = '34:4278f8122dfb723f87dcddf3714a70f2';
$prid = (int)zen_get_prid($uprid);

$not = ($prid != $uprid) ? ' not' : '';
echo "'$prid' is$not equal to '$uprid'<br>";

$not = (((string)$prid) != $uprid) ? ' not' : '';
echo "(string)'$prid' is$not equal to '$uprid'";

require DIR_WS_INCLUDES . 'application_bottom.php';

Create a .php file in the root of your Zen Cart (e.g. test.php) and run it as mystore.com/test.php; you'll be surprised at the output!