I did a if thing to check if the variable only had aplha caracter but I also want to check if the variable strated with a vowel.
if (ctype_alpha($original)) {
print "oké";
}
else {
print "pas oké";
}
If there isn't any specific function, how could I know ?
Thank you
$vocals = array('a','e','i','o','u');
if (ctype_alpha($original) && in_array($original{0}, $vocals)) {
print "oké";
} else {
print "pas oké";
}
With $string{index} you could access to a char into a certain position into $string.
With in_array you could check if that letter is contained into an array (in our example, vocals array)
Alternative solution
If you wish, you could even use preg_match that will perform test with regular expressions
if (ctype_alpha($original) && preg_match('/^[aeiou]/i', $original))
Working as of PHP 7.4:
$vowels = array('a','e','i','o','u');
if (ctype_alpha($original) && in_array($original[0], $vowels)) {
print "oké";
} else {
print "pas oké";
}
Related
The problem is with this line:
if $var LIKE '1800%';
and I'm not sure how to fix it. Thanks.
<?php
//check to see if account number is like 1800*
if (isset($_POST['acct_number'])) {
$var = $_POST['acct_number'];
if $var LIKE '1800%'; {
//stop the code
exit;
} else {
echo 'normal account number';
}
}
?>
You need PHP not MySQL. For 1800% just check that it is found at position 0:
if(strpos($var, '1800') === 0) {
//stop the code
exit;
} else {
echo 'normal account number';
}
If it can occur anywhere like %1800% then:
if(strpos($var, '1800') !== false) {
//stop the code
exit;
} else {
echo 'normal account number';
}
Use substr function to get first 4 characters and compare it with 1800.
if(substr($var, 0, 4) == '1800')
{
// your code goes here.
}
``
Another way could be to use strpos()
if (strpos($var, '1800') === 0) {
// var starts with '1800'
}
I would use a regular expression for this preg_match('/^1800.+/', $search, $matches);
In my php I have 2 optional inputs. input1= and input2=. Both are optional inputs. My question is how do I determine if an input was just not provided or if the input provided was not a string?
I only want people to put an actual string. Not different types of data structures.
Examples
Valid: www.example.com/myfile.php?input1=hello&input2=bye
Valid: www.example.com/myfile.php?input1=hello
Valid: www.example.com/myfile.php?input2=hello
Valid: www.example.com/myfile.php
Invalid: www.example.com/myfile.php?input1[]
<?php
function check_valid($string) {
if (!is_string($string)) {
echo "This is a not string. We tested: ".$string."<br>";
} else {
echo "This is is string. We tested: ".$string."<br>";
}
}
$input1 = check_valid($_GET['input1']);
$input2 = check_valid($_GET['input2']);
?>
You should use the isset() method to check if $input1 or $input2 are provided or not.
function check_valid($test) {
if (isset($test) && is_string($test)) {
echo "Valid";
return;
}
echo "Not Valid";
}
You can check if the value exists, containts empty or null.
<?php
function check_valid($string) {
if (trim($string) == NULL OR trim($string) == "") { //Will check if value without a space is null or empty
return "valid";
}
}
$input1 = check_valid($_GET['input1']);
$input2 = check_valid($_GET['input2']);
?>
I have a question about an if statement.
if($_POST['billing_first_name'] == $tet['data']['0']['first_name']) {
}
else
{
run code
}
This code compares a string that it gets from a form with an already existing string.
It works. But when i dont add a capital letter, it runs the code after all.
So for example if i compare String with String it doesnt run the code (which is what i want) But when i compare string with String it runs the code (which i dont want) Only because theres no capital letter included at the beginning. Is there a way to fix this?
Based on the comment the code is running fine it is an understanding of which block your are in.
if(condition){//code} will only run code when the condition is true ie
if("String" == "String"){echo "foo";} // will echo foo
if("string" == "String"){echo "foo";} // will not echo foo
In order to run code when false you add an else
// Will echo foo
if("String" == "String"){
echo "foo";
} else {
echo "bar";
}
// Will echo bar
if("string" == "String"){
echo "foo";
} else {
echo "bar";
}
A trick to getting ifs to execute when something is false is to use the ! (meaning not)
// Will echo bar
if("String" != "String"){
echo "foo";
} else {
echo "bar";
}
// Will echo foo
if("string" != "String"){
echo "foo";
} else {
echo "bar";
}
I solved it thanks to cmorrissey.
using strlower on both variables allows me to compare 2 strings no matter if there are capital letters inside one of the strings.
if(strtolower($_POST['billing_first_name']) == strtolower($tet['data']['0']['first_name'])) {
//do nothing
}else {
//run code
}
How to compare 2 variable using php like this ?
$aaa = "1234567890qwertyuiopsdflkjwerouioiuweewjkee";
$bbb = "1234567890qwertyuiop";
How to check
if(first char to twenty char of $aaa == $bbb)
{ echo "same"; }
else
{ echo "not same"; }
I assume you are searching for strncmp:
This function is similar to strcmp(), with the difference that you can specify the (upper limit of the) number of characters from each string to be used in the comparison.
if(strncmp($aaa, $bbb, 20) == 0) {
# First twenty characters match.
} else {
# First twenty characters don't match.
}
$aaafirst20 = $small = substr($aaa, 0, 20);
if(strcmp($aaafirst20 , $bbb){
}
else{
}
Try this :
You can use strcmp function for the same.
PHP docs # strcmp
if(strcmp($aaa,$bbb)){
echo "same";
} else {
echo "not same";
}
You can use strpos() to check if $bbb is found in $aaa, and starts at position 0.
if (strpos($aaa, $bbb) === 0) {
echo 'Same';
}
else echo 'Not same';
See demo
Your exact solution would be
// first reduce a to its first 20 characters
$trimmed = substr($aaa, 0, 20);
// now compare with b
if($trimmed == $bbb){
// same
}
Or, all in one line
if(substr($aaa, 0, 20) == $bbb){
// same
}
my problem is, i have a form which i fill blabla and after i submit i need to check if the var '$number' contains only 9 numbers. which means that if it contains at least 1 letter or has less or more than 9 length it should return false, else it should return true;
this is what i got so far:
if (!is_numeric ($number) {
//do
} else {
}
1st problem: This code should take care of the only numbers part but it doesnt, it always returns false.
2nd: do you guys know of any way to take care of the 9 digits only verification?
thanks and sorry for my bad english, not my native language :P
Your number may contain unwanted whitespaces which cause the is_numeric() test not to work properly
So do the following: $number = trim($number); to remove them.
Then indeed this snippet is good to check if your variable is a number:
if (!is_numeric ($number)) {
//do
} else {
}
And for the number digits do a if statement to see if your number is between 100000000 and 999999999
So the full code will be:
$number = trim($number);
if (!is_numeric ($number)) {
//do
} else {
if ($number >= 100000000 && $number <= 999999999) {
// Everything is ok
} else {
}
}
Didn't understood your complete question coz of you native language :p, but i think you want this:
if (is_numeric($number) {
if(strlen($number) == 9){
return true;
} else {
return false;
}
} else {
echo 'Not a number';
}
Check if it contains digits and check whether its exactly contains 9.
$number = '123456789';
if(!preg_match('/^\d{9}$/', $number)) {
echo 'not ok';
} else {
echo 'ok';
}