my code is
<?php if($_SERVER['REQUEST_URI'] == "/scripts/script1.php")
{
echo 'yes';
}
else
{
echo 'no';
}
some time my URL come like
www.example.com/scripts/script1.php?var1=value1&var2=value2
How can do that with PHP
URL like function....
Make use of strpos in PHP
<?php
if(strpos($_SERVER['REQUEST_URI'],"/scripts/script1.php")!==false)
{
echo 'yes';
}
else
{
echo 'no';
}
A slight variation on Shankar's answer
$result = "no";
if( strpos( $_SERVER[ "REQUEST_URI" ], "/scripts/script1.php" ) !== false ) {
$result = "yes";
}
By setting the default value for the variable, you can make your code more concise and improve readability.
Related
I am checking if two values are null. If both are null I want to return false, if either or both are not null, I want to return true.
My current code returns true only when both are not null but I want it to return true when either or not null.
// check if both null
if (!isset($myarray['dataone'], $myarray['datatwo']))
{
echo 'false';
);
} else {
echo 'true';
);
}
return $emptytabs;
For that you can use relational operators. AND (&&) OR (||)
By using AND (&&) operators.
if ( (!isset($myarray['dataone']) || (!isset$myarray['datatwo'] ))
{
echo 'false';
}
else
{
echo 'true';
}
By using OR ( || ) operators.
if (isset($myarray['dataone'] && isset$myarray['datatwo'])
{
echo 'false';
}
else
{
echo 'true';
}
// check if both null
if ( !isset($myarray['dataone']) && !isset($myarray['datatwo'])) {
echo 'false';
} else {
echo 'true';
}
// check if one or both are null
if ( !isset($myarray['dataone']) || !isset($myarray['datatwo'])) {
echo 'false';
} else {
echo 'true';
}
// check if both null
if ( !isset($myarray['dataone'], $myarray['datatwo']) )
{
echo 'false';
} else {
echo 'true';
}
return $emptytabs;
this approach you provided is totally true , but it only return true if all the provided parameters are set according to php documentations .
so your code should works correctly . except you have unwanted parentheses that should deleted
The simplest way is to use the OR (||) operator. You want to show 'true' if one thing is set OR another thing is set. Just say that with code.
if ( isset($myarray['dataone']) || isset($myarray['datatwo']) ) {
echo 'true';
} else {
echo 'false';
}
Using the AND operator adds pointless complexity by checking that both of the two things are not set in order for it to show 'false'. That's not an intuitive way to think about it, so it doesn't make sense to write the code that way.
DISCLAIMER: This answer is opinionated.
I'm fairly new to PHP so forgive me if this function is badly done.
I have a function:
function socialLink($sm_type = NULL) {
if ($sm_type = 'twitter') {
echo 'https://www.twitter.com';
} else {
echo 'https://www.facebook.com';
}
}
In my code when I call the function socialLink('facebook'); it echo's the Twitter URL.
Surely it should echo the Facebook URL since $sm_type would be equal to 'facebook' not twitter ?
Any help would be appreciated.
Set your if condition with this,
function socialLink($sm_type = NULL) {
if ($sm_type == 'twitter') {
echo 'https://www.twitter.com';
} else {
echo 'https://www.facebook.com';
}
}
See this.
function socialLink($sm_type = NULL) {
if ($sm_type == 'twitter') {
echo 'https://www.twitter.com';
} else {
echo 'https://www.facebook.com';
}
}
NOTE: Single = use to assign the value and = = use to compare values
Different's Between = , = = , = = =
= operator Used to just assign the value.
= = operator Used to just compares the values not datatype
= = = operator Used to Compare the values as well as datatype.
Your if statement does not use a comparison operator, it is an assignment (=). For a comparison, please use "==".
if ($sm_type == 'twitter') {
echo 'https://www.twitter.com';
} else {
echo 'https://www.facebook.com';
}
if ($sm_type == 'twitter') {
echo 'https://www.twitter.com';
} else {
echo 'https://www.facebook.com';
}
In php == is use for string comparison so, In this case you can't used = for that, simple :)
Following is my code here actually o/p should be hi..but it is giving no
<?php
$arr=array('Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday');
$c='xyz,ccc';
if(in_array(isset($c) && $c,$arr))
{
echo 'hi';
}
else
{
echo 'no';
}
?>
output:hi
actual result should be 'no'.
Side note, this is bad code:
in_array(isset($weekendArr) && $weekendArr,$arr)
do it like
isset($weekendArr) && in_array($weekendArr,$arr)
and in_array is not strict so this
in_array(true,array('w','s'))
will be allways TRUE
do it with:
in_array(true,array('w','s'),true)
and you see.
And you can't check an array with an array the $needle be an STRING here.
The only solution is to do splitt your STRING into two values and then check two times for TRUE
$c='Sunday,Monday';
foreach(explode(',',$c) as $check){
if(in_array($c,$arr,true))
{
echo $check.' is in array';
}
else
{
echo $check.' is NOT in array';
}
}
Hope that helps a little.
<?php
$listDays=array('Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday');
$day='Sunday'; //You cant test both days ! Just one value at a time
if(true === in_array($day, $listDays))
{
echo 'hi';
}
else
{
echo 'no';
}
?>
Or option two if you want to test different days
<?php
$listDays=array('Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday');
$dayToTest='Sunday, Monday'; //Here we have multiple days
$tabTest = preg_split(',', $day); //split into an array
//Then test for each string in tabTest
foreach($tabTest as $string)
{
if(true === in_array($string, $listDays))
{
echo $string.' is OK';
}
else
{
echo 'no';
}
}
?>
Change your code to:
<?php
$arr=array('Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday');
$c='Sunday,Monday';
if(in_array(isset($c) && $c,$arr))
{
echo 'hi';
}
else
{
echo 'no';
}
very short simple quiz app. browser keeps returning Correct as a response, even though it is not correct.
<?php
$answer = (isset($_POST['answer']));`
if ($answer == "Dagny Taggart")`
{
echo "Correct";
} else {
echo "wrong";
}
?>
Try this. You're currently setting $answer to isset($_POST['answer'], meaning as long as $_POST['answer'] is set, you're $answer is going to be TRUE. I would check if it's set and then set it to the $_POST value.
<?php
if(isset($_POST['answer'])) {
$answer = $_POST['answer'];
if ($answer == "Dagny Taggart")
{
echo "Correct";
} else {
echo "wrong";
}
} else {
// Do something?
}
?>
isset returns a boolean (true or false) value. So your test will always fail because true != 'Dagny Taggart'
Add this ternary and it will work
$answer = (isset($_POST['answer'])) ? $_POST['answer'] : '';
If you have 10 variables that are sometimes set, other times unset, is there a quick way to echo the ones that exist without throwing an exception? These vars come from user input.
I would currently write it as
if ($var_1 != NULL) { echo $var_1; }
if ($var_2 != NULL) { echo $var_2; }
if ($var_3 != NULL) { echo $var_3; }
if ($var_other_1 != NULL) { echo $var_other_1 ; }
if ($var_other_2 != NULL) { echo $var_other_2 ; }
etc.. But is there a more quicker way?
compact function will help you
Check this function: http://php.net/manual/en/function.get-defined-vars.php
You can do something like this:
<?php
$vararr = get_defined_vars();
foreach ($vararr as $name => $value) {
echo "{$name}: {$value}<br>\n";
}
Here's another option using variable variables and a list of the variables you want to examine:
foreach( array("var_1", "var_2") as $var )
{
if( isset($$var) )
{
echo $$var;
}
}