Check the start value of a variable

0

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!";
}
?>
    
asked by anonymous 26.06.2018 / 16:48

1 answer

2

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 .

    
26.06.2018 / 17:35