Brandin Arsenault Brandin Arsenault - 1 month ago 5
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

addorder.php
it knows to grab the
date
and
ordernumber
using post, however when it does that, I need it to perform an IF function where if
$ordernumber
starts with
7
the value of
$system = E
or if it starts with
9
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
$ordernumber
string to verify which criteria it meets.

Answer

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';
}
Comments