I want to check if the initial value of a variable is "such". How can I do this? Here is an example for a better understanding:
<?php
$teste = "(51) 3212-3212";
if ($teste =="(51)%") {
echo "DEU CERTO!";
}
else {
echo "DEU RUIM!";
}
?>
I want to check if the initial value of a variable is "such". How can I do this? Here is an example for a better understanding:
<?php
$teste = "(51) 3212-3212";
if ($teste =="(51)%") {
echo "DEU CERTO!";
}
else {
echo "DEU RUIM!";
}
?>
You can use the strpos
function. It will return you to the position where determined string
was found in its $teste
variable. If you return 0
, it means that this string is at the beginning.
$teste = 'minha string';
if (strpos($teste, 'minha') === 0) {
echo "'minha' está no início";
}
Translating PHP documentation :
int strpos ( string $palheiro , string $agulha [, int $posição ] )
Returns the numeric position of the first occurrence of
$agulha
within$palheiro
.