Example: I have a $variable = "_foo"
, and I want to make absolutely sure that $variable does not start with an underscore "_"
. How can I do that in PHP? Is there some access to the char array behind the string?
To check if string starts with a specific character, use PHP built-in function strpos(). Provide the string and character as arguments to strpos(), and if strpos() returns 0, then we can confirm that the string starts with the specific character, else not.
You can use the PHP strpos() function to check whether a string contains a specific word or not. The strpos() function returns the position of the first occurrence of a substring in a string. If the substring is not found it returns false .
To check if string ends with specific substring, use strcmp() function to compare the given substring and the string from a specific position of the string. Take string in variable $string. Take substring in $substring. Compute the length of $substring and store it in $length.
Value Type: Boolean. Example: <? php $var_name=127.55; if (is_double($var_name)) echo 'This is a double value.
$variable[0] != "_"
In PHP you can get particular character of a string with array index notation. $variable[0]
is the first character of a string (if $variable is a string).
You might check out the substr
function in php and grab the first character that way:
http://php.net/manual/en/function.substr.php
if (substr('_abcdef', 0, 1) === '_') { ... }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With