Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
$nopa = "Shotgun";
$weapon1 = array("Ingen","Shotgun","Glock 17","Revolver","AK 47","Barrett M82");
How can i do so this code will output a yes if the $weapon is equal to $nopa, else it would output a no. Any ideas?
you can use in_array from PHP documentation stated here: http://php.net/manual/en/function.in-array.php
Check the PHP function "in_array"
http://php.net/manual/en/function.in-array.php
if ( in_array($stringVar, $arrayVar) ) {
//do something
}
Careful about case, however.
If needle is a string, the comparison is done in a case-sensitive
manner.
check out the built-in in_array function:
http://php.net/manual/en/function.in-array.php
this is what You want:
<?
$nopa = "Shotgun";
$weapon1 = array("Ingen","Shotgun","Glock 17","Revolver","AK 47","Barrett M82");
if (in_array($nopa,$weapon1)){
echo 'YES';
} else {
echo 'NO';
}
?>
WORKING CODE
Related
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
How do I make the following url to be read:
localhost/index.php?errormsg=wrongpassword
so I want it to set the variable $errormsg to 'wrongpassword' how do I do that? I am pretty sure there is a way using $_GET or $_POST?
You want to use $_GET['errormsg'].
Something like this should do:
if isset($_GET['errormsg'])
{
echo $_GET['errormsg'];
}
$errormsg = $_GET['errormsg']
This should do the trick for you
$_GET['errormsg'] is the answer of your question.
print_r($_GET)
to see all variables in the URL. in this case you want
$_GET['errormsg']
You were as close as ever. A single google would've yielded the answer to you :)
http://php.net/manual/en/reserved.variables.get.php
<?php
// use htmlspecialchars() to clean up url-encoded characters
$error_message = htmlspecialchars($_GET["errormsg"]);
echo 'Error: ' . $error_message;
?>
prints: "Error: error_here", if url = ...?errormsg=error_here
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
Sorry for another noob question, but... Can someone please explain to me what the function myfunction is actually doing. I understand it's checking if the variables $a and $b are identical and than it's suppose to return 0 if they're identical but the next return is confusing. I see their using the ternary operators.
function myfunction($a,$b)
{
if ($a===$b)
{
return 0;
}
return ($a>$b)?1:-1;
}
$a1=array("a"=>"red","b"=>"green","c"=>"blue");
$a2=array("a"=>"red","b"=>"green","d"=>"blue");
$a3=array("e"=>"yellow","a"=>"red","d"=>"blue");
$result=array_diff_uassoc($a1,$a2,$a3,"myfunction");
print_r($result);
the print_r returns
Array ( [c] => blue )
but how did we get here...
As stated in the documentation of array_diff_uassoc, it returns the entries from first argument that are unique compared to other arguments. And the last argument is the name of the function it uses to check whether item is unique or not.
So because only $a1 contains 'c'=>'blue' it is returned.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I'm trying to set the class hidden, unless two factors are met. Currently, I'm using the code below:
<?php
if (isset($_POST['prerequisite']) && $form == "CheckingIn")
{
}
else
{
echo "hidden";
}
?>
How can I fix this to just be an if statement instead of an if/else?
Negate the expression; use the "NOT" logical operator:
if (!(isset($_POST['prerequisite']) && $form == "CheckingIn")) { echo "hidden"; }
(notice the ! symbol)
Read more here: http://php.net/manual/en/language.operators.logical.php
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
$a='Would you like to have responses to your questions sent to you via email'
for($a as $b=>c){
}
Kindly give me the solution
<?php echo substr_count($a, 't'); ?>
Here's a solution, though not necessarily the best
$letterCount = array_count_values(
str_split(strtolower($a))
);
$tCount = (isset($letterCount['t'])) ? $letterCount['t'] : 0;
$count = preg_match_all('/t/', $str);
check this one
<?php
$a = 'Would you like to have responses to your questions sent to you via email';
$t_explod = explode('t', strtolower($a));
echo count($t_explod) - 1;
?>
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
The simple program is not returning the string,Please help.
<?php
function returnStr() {
return "fooBar";
}
$str=returnStr();
echo $str;
}
?>
It's a parse error:
$str=returnStr();
echo $str;
} // WHAT IS THIS BRACKET DOING HERE?
There's a fatal error tokenizing the code due the trailing/unmatched '}' remove that and the code will work. Then spend some time thinking about why you didn't know this already
There's an error in your code. The last } is useless.