To make `no-variable` if-clause in PHP

By : Masi
Source: Stackoverflow.com
Question!

How can you make the if -clause which do this and this if

  1. there is no variable in the URL by PHP?
  2. there is no variable which has value in the URL by PHP?

Examples of the URLs which should be true for the if -clause for #1:

www.example.com/index.php
example.com/index.php
example.com/index.php?

Examples of the URLs which should be true for the if -clause for #2:

example.com/index.php?successful_registration
example.com/index.php?successful_login

The following is my unsuccessful if -clause for $1

if (isset($_REQUEST[''])) {
      // do this
}
By : Masi


Answers

If statements are not loops. You might call it an condition though.

if(isset($_REQUEST['successful_registration'])) {
  //do this
}elseif(isset($_REQUEST['successful_login'])) {
  //do this
}else{
  //do this
}


You'd want to check $_GET and $_SERVER["query_string"].

If the query string is empty, you've got just the base url. If the query string is not empty, but one of the $_GET variables is empty, you've got an invalid domain.

By : Sampson


if ( 0 == count( $_GET ) )
{
  // do this
}

or

if ( empty( array_keys( $_GET ) ) )
{
  // do this
}

or

if ( '' == $_SERVER['QUERY_STRING'] )
{
  // do this
}


This video can help you solving your question :)
By: admin