how can I keep the p= and add an OR /20 on it ? I mean I want to check both strings.
<?php if ( false==stripos(get_permalink($post->ID), 'p=') ) { ?>
You cannot test for two portions of strings with a single stripos() call : you have to call stripos() twice.
Depending on what you exactly want to achieve (not sure I really undertand the question), you'll combine those two with && or || :
$link = get_permalink($post->ID);
if (stripos($link, 'p=')!==false && stripos($link, '/20')!==false) {
// the link contains both p= AND /20
}
or :
$link = get_permalink($post->ID);
if (stripos($link, 'p=')!==false || stripos($link, '/20')!==false) {
// the link contains p= OR (inclusive) /20
}
As a sidenote : you should use === or !==, as stripos() can return 0 or false -- and those don't have the same meaning.
You have to call it twice:
if( false===stripos(get_permalink($post->ID), 'p=') ||
false===stripos(get_permalink($post->ID), '/20')
) {
Related
if(!isset($_GET['new_quiz']) || !isset($_GET['view_quiz']) || !isset($_GET['alter_quiz'])){
echo "No";
}
else{ echo "Yes"; }
When I go to index.php?view_quiz, it should give result as Yes, but it results as No. Why?
My Other Tries:
(!isset($_GET['new_quiz'] || $_GET['view_quiz'] || $_GET['alter_quiz']))
( ! ) Fatal error: Cannot use isset() on the result of an expression
(you can use "null !== expression" instead) in
C:\wamp\www\jainvidhya\subdomains\teacher\quiz.php on line 94
(!isset($_GET['new_quiz'],$_GET['view_quiz'],$_GET['alter_quiz']))
NO
You may find than inverting the logic makes the code easier to read, I also like to have a more positive idea of conditions as it can read easier (rather than several nots means no).
So this says if anyone of the items isset() then the answer is Yes...
if(isset($_GET['new_quiz']) || isset($_GET['view_quiz']) || isset($_GET['alter_quiz'])){
echo "Yes";
}
else{ echo "No"; }
Note that I've changed the Yes and No branches of the if around.
You are probably looking for
if(!isset($_GET['new_quiz']) && !isset($_GET['view_quiz']) && !isset($_GET['alter_quiz'])){
echo "No";
}
else {
echo "Yes";
}
which will print Yes if none of new_quiz, view_quiz and alter_quiz are present in the URL. If this is not your desired outcome, please elaborate on your problem.
#paran you need to set a value for view_quiz=yes for example
if(!isset($_GET['new_quiz']) || !isset($_GET['view_quiz']) || !isset($_GET['alter_quiz'])){
echo "No";
}
else{ echo "Yes"; }
and the url
index.php?new_quiz=yes
index.php?view_quiz=yes
index.php?alter_quiz=yes
All Will return true
isset()allows multiple params. If at least 1 param does not exist (or is NULL), isset() returns false. If all params exist, isset() return true.
So try this:
if( !isset( $_GET['new_quiz'], $_GET['view_quiz'], $_GET['alter_quiz']) ) {
First, to answer your question:
When I go to index.php?view_quiz, it should give result as Yes, but it results as No. Why?
This is becaue this
if(!isset($_GET['new_quiz']) || !isset($_GET['view_quiz']) || !isset($_GET['alter_quiz'])){
checks if either one of your parameter is not set, which will always be the case as long as you are not setting all three parameter simultaneously like this:
index.php?alter_quiz&view_quiz&new_quiz
As #nigel-ren stated, you may wan't to change that logic to
if(isset($_GET['new_quiz']) || isset($_GET['view_quiz']) || isset($_GET['alter_quiz'])){
echo 'Yes';
which checks if at least one parameter is set.
If you wan't to check if there is only one of the three parameters set, you would have to work with XOR (which is slightly more complicated)
$a = isset($_GET['new_quiz']);
$b = isset($_GET['view_quiz']);
$c = isset($_GET['alter_quiz']);
if( ($a xor $b xor $c) && !($a && $b && $c) ){
echo 'Yes';
(based on this answer: XOR of three values)
which would return true if one and only one of the three parameters is set.
But - and this is just an assumption, please correct me if I'm wrong - I think what you are trying to achieve are three different pages (one for creating a quiz, one for viewing it and one for editing it). Therefore, you will likely run into a problem with your current setup. For example: What would happen if a user calls the page with multiple parameters, like
index.php?alter_quiz&view_quiz
Would you show both pages? Would you ignore one parameter? I would recommend to work with a single parameter to avoid this problem in the first place. For example site which can take the values alter_quiz, view_quiz or new_quiz. E.g.:
index.php?site=alter_quiz
Then you can work like this:
// check if site is set before getting its value
$site = array_key_exists( 'site', $_GET ) ? $_GET['site'] : NULL;
// if it's not set e.g. index.php without parameters is called
if( is_null($site) ){
// show the start page or something
}else{
$allowed_sites = ['new_quiz', 'view_quiz', 'alter_quiz'];
// never trust user input, check if
// site is an allowed value
if( !in_array($site, $allowed_sites, true) ){
die('404 - This site is no available');
}
// here you can do whatever your site should do
// e.g. include another php script which contains
// your site
include('path/to/your/site-' . $site . '.php');
// or echo yes
echo 'Yes';
}
Current Code:
<?php
// See the AND operator; How do I simplify/shorten this line?
if( $some_variable !== 'uk' && $some_variable !== 'in' ) {
// Do something
}
?>
And:
<?php
// See the OR operator; How do I simplify/shorten this line?
if( $some_variable !== 'uk' || $some_variable !== 'in' ) {
// Do something else
}
?>
Is there a simpler (i.e. shorter) way to write the two conditions?
NOTE: Yes, they are different, and I am expecting different ways to shorten the codes.
For your first code, you can use a short alteration of the answer given by
#ShankarDamodaran using in_array():
if ( !in_array($some_variable, array('uk','in'), true ) ) {
or even shorter with [] notation available since php 5.4 as pointed out by #Forty in the comments
if ( !in_array($some_variable, ['uk','in'], true ) ) {
is the same as:
if ( $some_variable !== 'uk' && $some_variable !== 'in' ) {
... but shorter. Especially if you compare more than just 'uk' and 'in'.
I do not use an additional variable (Shankar used $os) but instead define the array in the if statement. Some might find that dirty, i find it quick and neat :D
The problem with your second code is that it can easily be exchanged with just TRUE since:
if (true) {
equals
if ( $some_variable !== 'uk' || $some_variable !== 'in' ) {
You are asking if the value of a string is not A or Not B. If it is A, it is definitely not also B and if it is B it is definitely not A. And if it is C or literally anything else, it is also not A and not B. So that statement always (not taking into account schrödingers law here) returns true.
You can make use of in_array() in PHP.
$os = array("uk", "us"); // You can set multiple check conditions here
if (in_array("uk", $os)) //Founds a match !
{
echo "Got you";
}
I like the code used by Habchi in comments
if(!($some_variable === 'uk' || $another_variable === 'in')){//do}
An alternative that might make sense especially if this test is being made multiple times and you are running PHP 7+ and have installed the Set class is:
use Ds\Set;
$strings = new Set(['uk', 'in']);
if (!$strings->contains($some_variable)) {
Or on any version of PHP you can use an associative array to simulate a set:
$strings = ['uk' => 1, 'in' => 1];
if (!isset($strings[$some_variable])) {
There is additional overhead in creating the set but each test then becomes an O(1) operation. Of course the savings becomes greater the longer the list of strings being compared is.
If you're planning on building a function in the if statement, I'd also advise the use of in_array. It's a lot cleaner.
If you're attempting to assign values to variables you can use the if/else shorthand:
$variable_to_fill = $some_variable !== 'uk' ? false : true;
You need to multi value check. Try using the following code :
<?php
$illstack=array(...............);
$val=array('uk','bn','in');
if(count(array_intersect($illstack,$val))===count($val)){ // all of $val is in $illstack}
?>
You may find it more readable to reverse your logic and use an else statement with an empty if.
if($some_variable === 'uk' || $another_variable === 'in'){}
else {
// This occurs when neither of the above are true
}
Some basic regex would do the trick nicely for $some_variable !== 'uk' && $some_variable !== 'in':
if(!preg_match('/^uk|in$/', $some_variable)) {
// Do something
}
I currently have this:
$thispage = $_SERVER['QUERY_STRING'];
if($thispage == "i=f" || $thispage == "i=f&p=t" ) echo "active";
Although, the "i=f&p=t" is not correct, as the page looks like this:
i=f&p=t&cid=71&id=161
My question is, how can I do so if just the first part of the $_GET string is correct (in this example "i=f&p=t) then it will match $thispage. I don't want it to check for & $_GETs
I want $thispage to work with other pages than just the example above. I only want to check the first part of the string. Not what comes after &
I'd go for parse_str function:
parse_str($_SERVER['QUERY_STRING'], $thispage);
if ( $thispage['i'] === 'f' OR ($thispage['i'] === 'f' AND $thispage['p'] === 't') ) {
echo 'active';
}
Update:
I don't want it to check for & $_GETs
I don't know why you don't want to use $_GET if it's the Query String that you're working on that now, but you can do the following as well, which makes more sense:
if ( $_GET['i'] === 'f' OR ($_GET['i'] === 'f' AND $_GET['p'] === 't') ) {
echo 'active';
}
Did you mean this?
if(strpos($_SERVER['QUERY_STRING'], 'i=f') !== false) {
echo "active";
}
If that is truly what you want to do, then you are actually checking if
if (substr($_SERVER['QUERY_STRING'], 0, 7) === "i=f&p=t")
You can access the url parameters directly by using the $_GET array.
var_dump($_GET);
If you want to use $_SERVER['QUERY_STRING'] you can use the parse_str function to parse the query string:
<?php
$str = "first=value&arr[]=foo+bar&arr[]=baz";
parse_str($str, $output);
echo $output['first']; // value
echo $output['arr'][0]; // foo bar
echo $output['arr'][1]; // baz
?>
I'm trying to match a condition where if the user status is 10 and ANY POST variables are not set it triggers an error:
if ($_SESSION['status']=='10' && !isset($_POST['a']) || !isset($_POST['B'])) {}
I can not use && conditions for any !isset as one variable may be set though another might not. I only want the condition to match if one or more variables are not set AND the status==10.
When testing if a $_POST variable !isset, I remove an input element from the page via a browser web tool (e.g. Firebug). When the form is submitted with the variable missing it's still passing validation incorrectly.
I am also seeking a PHP if grouping condition.
If you are looking for absolutely any PHP variables, I'd recommend this:
if (($_SESSION['status'] == 10) && (count($_POST) > 0)) {
You can then get the list of _POST var keys using array_keys($_POST).
If you are looking for a specific:
if (($_SESSION['status'] == 10) && (isset($_POST['A']) || isset($_POST['b']))) {
The order of the brackets is important. You can separate groups of logical statements with brackets.
Is that was what you were looking for?
$status = $_SESSION['status'];
if($status == '10'){
if(!isset($_POST['a']) or !isset($_POST['B'])){
//Triggers error.
}else{
//Another
}
}
Try making it a function:
function checkAllVars($dataVars, $requestVars) {
foreach($dataVars as $varname) {
if(!isset($requestVars[$varname])) {
return false;
}
}
return true;
}
$dataVars = array (
"varName1",
"varName2",
"varName3",
"varName4",
);
$allVarsSet = checkAllVars($dataVars, $_REQUEST);
you might be looking for
if($_SESSION['status']=='10' && (!isset($_POST['a']) || !isset($_POST['B']))){}
^ ^
which means if status = 10 and (if not set 'a' or not set 'B' or they can be both not set) do something
or you might be looking for
if(($_SESSION['status']=='10' && !isset($_POST['a'])) || ($_SESSION['status']=='10' && !isset($_POST['B']))){}
I have system, that using keywords for some data
There are normal keywords and meta keywords - To:all, Tomember: and Togroup:
and I have following condition to check meta keywords:
if ((strpos($kwd, 'To:all') === 0) ||
(strpos($kwd, 'Tomember:') === 0) ||
(strpos($kwd, 'Togroup:') === 0))
{
/* ... */
}
I think this way of identifying meta keywords is incorrect.
One more incorrect way is like this:
if ((strpos($kwd, 'To:all') !== FALSE) ||
(strpos($kwd, 'Tomember:') !== FALSE) ||
(strpos($kwd, 'Togroup:') !== FALSE))
{
/* ... */
}
And in my opinion the correct way is:
if ((substr($kwd,0,6) == 'To:all') ||
(substr($kwd,0,9) == 'Tomember:') ||
(substr($kwd,0,8) == 'Togroup:'))
{
/* ... */
}
Any thoughts?
Of the solutions you propose, the second is wrong because it will return true even if the meta-keywords do not appear in the beginning of $kwd. The other two work correctly.
An even better way would be:
function str_starts_with($haystack, $needle) {
return substr($haystack, 0, strlen($needle)) == $needle;
}
if (str_starts_with($kwd, 'To:all') ||
str_starts_with($kwd, 'Tomember:') ||
str_starts_with($kwd, 'Togroup:'))
{
// ...
}
strpos($kwd, 'To:all') === 0
will check if the $kwd string begins with To:all -- it'll check if the position of To:all in $kwd is 0.
strpos($kwd, 'To:all') !== FALSE
will check if the $kwd string contains To:all -- no matter at which position.
substr($kwd,0,6) == 'To:all'
whill check if the first 6 characters of $kwd are To:all -- which is equivalent to the first solution.
If you want to test the begins with case, you'll use the first or third solution.
Personnaly, I'd go with the strpos-based : I find it easier to read/understand ; but it's mainly a matter of personnal preferences.
If you want to test the contains case, you'll need to use the second solution.