How can I use two or more strings?
This is what I'm using to do just one string.
if ($input == "test")
{
echo "$imput is equal to test.";
}
If I understand you correctly, something like this might work best if you have many strings to compare with.
$string = array("foo", "bar", "hello", "world");
foreach ($string as $s) {
if ($input == $s) {
echo "$input is equal to $s";
break;
}
}
I think this is what you're looking for:
if ($input == "test" && $input2 == "hello")
{
echo "$input is equal to test and input2 is equal to hello.";
}
Do you mean how can you check if two strings contain a certain value?
If so, just use &&:
if($input == "test" && $input2 == "test") {
Then you can use
foreach ($dataarray as $string) {
if ($input == $string) {
echo "Match Found";
break;
}
}
Then it results Match Found if it founds the string in the array $dataarray();
Related
I have array like this :
$array = ["C","D","D#m","B","A","Am","A#m"];
How can I check my array items has this "#"?
Might be faster than a loop. Just create a string and check:
if(strpos(implode($array), '#') !== false) {
//yes
}
Or with a regex to check for any:
if(preg_grep('/#/', $array)) {
//yes
}
To get a count of the items:
if(count(preg_grep('/#/', $array)) == 3) {
//yes
}
Use below snippet of strpos to see if it exists '#',
foreach($arr as $v){
if(strpos($v, "#") !== false){
// do something
}
}
Demo.
I am trying to validate whether or not a string contains and starts with BA700. I have tried using the preg_match() function in PHP but I have not had any luck. My code is below:
preg_match('/^[0-9]{3}-[0-9]{4}-[0-9]{4}$/', $search))
This does not work unfortunately. Any ideas?
UPDATES CODE:
$needle = 'BA700';
$haystack = 'BA70012345';
if (stripos($haystack, $needle)) {
echo 'Found!';
}
This does not work for me either
Here is how to correctly use stripos
if (stripos($haystack, $needle) !== false) {
echo 'Found!';
}
Maybe I am taking this a little too literally, but:
if (strncmp($string, 'BA700', 5) === 0) {
// Contains and begins with 'BA700'
}
If the BA700 is case-insensitive then:
if (strncasecmp($string, 'ba700', 5) === 0) {
// Contains and begins with 'ba700'
}
There should not be much more to it than that.
The regular expression, in case you want to know, is:
if (preg_match('/^BA700/', $string) === 1) {
// Contains and begins with 'ba700'
}
Try with substr like
$needle = 'BA700';
$haystack = 'BA70012345';
if(substr($haystack, 0, 4) == $needle) {
echo "Valid";
} else {
echo "In Valid";
}
You can also check regards with the case by changing both of them in either UPPER or LOWER like
if(strtoupper(substr($haystack, 0, 4)) == $needle) {
echo "Valid";
} else {
echo "In Valid";
}
I have an switch with a random variable name and a array which can contain the values left, right, up and down.
For example:
switch ($i) {
case 0:
$name='something1';
$array=array('north', 'south', 'west');
break;
case 1:
$name='something2';
$array=array('north', 'south');
case 2:
$name='something3';
$array=array('south');
}
How can I make a script that checks for example if the only value in the array is 'south'? In my script the output will be something3, and if I check for the value's north and south, the script would output something2?
Hope you understand me. Thanks!
I would do:
if((count($array) == 1) && ($array[0] == 'south')){
//code here
}
This will only work if the array has one element.
Ok, I think this is a pretty foolproof way of accomplishing this:
<?php
function checktangent($array,$tocheck){
$tocheck = explode(',', str_replace(' ', '', $tocheck));
if(count($tocheck) == count($array)){
$foundall = true;
foreach($tocheck as $value){
if(!in_array($value, $array))
$foundall = false;
}
return $foundall;
}
else
return false;
}
//Use like:
$array = array('north', 'south', 'west');
if(checktangent($array, 'north, south'))
echo 'found';
else
echo 'not found'
?>
You can compare arrays directly in PHP. Be careful though, because this also compares the order of the values.
var_dump(array(1, 2) == array(1, 2)); //true
var_dump(array(1, 2) == array(2, 1)); //false
If you can guarantee the same order, you could do something like this:
<?php
$directions = array('north', 'south');
switch($directions) {
case array('north'):
echo 'north';
break;
case array('south'):
echo 'south';
break;
case array('north', 'south'):
echo 'north south';
break;
}
?>
http://codepad.viper-7.com/TCoiDw
This should work if I understand what your looking for correctly
if (false == count(array_diff(array('north', 'south', 'west'), $array))) {
echo 'something1';
} else if (false == count(array_diff(array('north', 'south'), $array))) {
echo 'something2';
} else if (count($array) == 1 AND current($array) = 'south') {
echo 'something3';
}
I think an easier solution would be:
array_unique($array);
if (count($array) == 1 && in_array('south', $array))
// Only south exists.
$textone = "pate"; //$_GET
$texttwo = "tape";
$texttre = "tapp";
if ($textone ??? $texttwo) {
echo "The two strings contain the same letters";
}
if ($textone ??? $texttre) {
echo "The two strings NOT contain the same letters";
}
What if statement am I looking for?
I suppose a solution could be to, considering the two following variables :
$textone = "pate";
$texttwo = "tape";
1. First, split the strings, to get two arrays of letters :
$arr1 = preg_split('//', $textone, -1, PREG_SPLIT_NO_EMPTY);
$arr2 = preg_split('//', $texttwo, -1, PREG_SPLIT_NO_EMPTY);
Note that, as pointed out by #Mike in his comment, instead of using preg_split() like I first did, for such a situation, one would be better off using str_split() :
$arr1 = str_split($textone);
$arr2 = str_split($texttwo);
2. Then, sort those array, so the letters are in alphabetical order :
sort($arr1);
sort($arr2);
3. After that, implode the arrays, to create words where all letters are in alphabetical order :
$text1Sorted = implode('', $arr1);
$text2Sorted = implode('', $arr2);
4. And, finally, compare those two words :
if ($text1Sorted == $text2Sorted) {
echo "$text1Sorted == $text2Sorted";
}
else {
echo "$text1Sorted != $text2Sorted";
}
Turning this idea into a comparison function would give you the following portion of code :
function compare($textone, $texttwo) {
$arr1 = str_split($textone);
$arr2 = str_split($texttwo);
sort($arr1);
sort($arr2);
$text1Sorted = implode('', $arr1);
$text2Sorted = implode('', $arr2);
if ($text1Sorted == $text2Sorted) {
echo "$text1Sorted == $text2Sorted<br />";
}
else {
echo "$text1Sorted != $text2Sorted<br />";
}
}
And calling that function on your two words :
compare("pate", "tape");
compare("pate", "tapp");
Would get you the following result :
aept == aept
aept != appt
use === and !==
if ($textone === $texttwo) {
echo "The two strings contain the same letters";
}else{
echo "The two strings NOT contain the same letters";
}
or
if ($textone === $texttwo) {
echo "The two strings contain the same letters";
}
if ($textone !== $texttwo) {
echo "The two strings NOT contain the same letters";
}
I have a variable$var.
I want echo "true" if $var is equal to any of the following values abc, def, hij, klm, or nop. Is there a way to do this with a single statement like &&??
An elegant way is building an array on the fly and using in_array():
if (in_array($var, array("abc", "def", "ghi")))
The switch statement is also an alternative:
switch ($var) {
case "abc":
case "def":
case "hij":
echo "yes";
break;
default:
echo "no";
}
if($var == "abc" || $var == "def" || ...)
{
echo "true";
}
Using "Or" instead of "And" would help here, i think
you can use in_array function of php
$array=array('abc', 'def', 'hij', 'klm', 'nop');
if (in_array($val,$array))
{
echo 'Value found';
}
Dont know, why you want to use &&. Theres an easier solution
echo in_array($var, array('abc', 'def', 'hij', 'klm', 'nop'))
? 'yes'
: 'no';
you can use the boolean operator or: ||
if($var == 'abc' || $var == 'def' || $var == 'hij' || $var == 'klm' || $var == 'nop'){
echo "true";
}
You can try this:
<?php
echo (($var=='abc' || $var=='def' || $var=='hij' || $var=='klm' || $var=='nop') ? "true" : "false");
?>
I found this method worked for me:
$thisproduct = "my_product_id";
$array=array("$product1", "$product2", "$product3", "$product4");
if (in_array($thisproduct,$array)) {
echo "Product found";
}
Sorry to resurrect this, but I stumbled across it & believe it adds value to the question.
In PHP 8.0.0^ you can now use the match expression like so:
<?php
echo match ($var) {
'abc','def','hij','klm' => 'true',
};
?>
//echos 'true' as a string
Working link from OnlinePHPfunctions
PHP Manual
Try this piece of code:
$first = $string[0];
if($first == 'A' || $first == 'E' || $first == 'I' || $first == 'O' || $first == 'U') {
$v='starts with vowel';
}
else {
$v='does not start with vowel';
}
It will be good to use array and compare each value 1 by 1 in loop. Its give advantage to change the length of your tests array. Write a function taking 2 parameters, 1 is test array and other one is the value to be tested.
$test_array = ('test1','test2', 'test3','test4');
for($i = 0; $i < count($test_array); $i++){
if($test_value == $test_array[$i]){
$ret_val = true;
break;
}
else{
$ret_val = false;
}
}
I don't know if $var is a string and you want to find only those expressions but here it goes either way.
Try to use preg_match http://php.net/manual/en/function.preg-match.php
if(preg_match('abc', $val) || preg_match('def', $val) || ...)
echo "true"