Brandin Arsenault Brandin Arsenault - 1 year ago 75
PHP Question

PHP if variable starts with

I am using PHP and MySQL to submit an order to a database. When the form submits to

it knows to grab the
using post, however when it does that, I need it to perform an IF function where if
starts with
the value of
$system = E
or if it starts with
the value of the
$system = O
(that is an "oh" not a zero).

I am unsure how to do this in PHP. It needs to only check the first digit in the
string to verify which criteria it meets.

Answer Source

You can use strval to treat a variable as a string, and then just use [0] to check the first digit:

$ch = strval($ordernumber)[0];
if ($ch == '7') {
    $system = 'E';
} elseif ($ch == '9') {
    $system = 'O';
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download