I'm trying to condense my code to create less lines of PHP and was wondering if this is possible. Currently I have:
if ($_SERVER['HTTP_HOST']==domain1.com" ) {
down();
} elseif ($_SERVER['HTTP_HOST']=="domain2.com" ) {
down();
} else {
up();
}
?>
instead of having if and elseif displaying the same function, is it possible to do something like this:
if ($_SERVER['HTTP_HOST']==domain1.com" || $_SERVER['HTTP_HOST']=="domain2.com" ) {
down();
} else {
up();
}
?>
I tried the code above but my page went blank.. how do I do this if possible?
Its because of the syntax errors -
if ($_SERVER['HTTP_HOST']=="domain1.com" || $_SERVER['HTTP_HOST']=="domain2.com" ) {
down();
} else {
up();
}
or you can do this also -
$domains = array("domain1.com", "domain2.com");
if (in_array($_SERVER['HTTP_HOST'], $domains)) {
Shorter way:
$domains = ["domain1.com", "domain2.com"];
in_array($_SERVER['HTTP_HOST'], $domains) ? down() : up();
You forgot the opening " for the first $_SERVER['HTTP_HOST']==domain1.com"
try with :
if ($_SERVER['HTTP_HOST']=="domain1.com" || $_SERVER['HTTP_HOST']=="domain2.com" ) {
down();
} else {
up();
}
Fix syntax errors please
if ($_SERVER['HTTP_HOST']=="domain1.com" || $_SERVER['HTTP_HOST']=="domain2.com" ) {
down();
} else {
up();
}
?>
Related
i want to ask, how can i shorten this code?
I've tried with ifelse but i dont get it..
I have this
if (empty($data['current_password'])) {
return false;
}
if (empty($data['new_password'])) {
return false;
}
if ($data['new_password'] !== $data['new_password_again']) {
return false;
}
Tried
if () {
} elseif (empty($data['current_password'])) {
} elseif ($data['new_password'] !== $data['new_password_again']) {
}
But doesnt work, please help
thank you very much
You can do like this
if (empty($data['current_password']) || empty($data['new_password']) || $data['new_password'] !== $data['new_password_again'])
{
return false;
}
If you like magical things, try this
switch (true) {
case (empty($data['current_password'])):
case (empty($data['new_password'])):
case ($data['new_password'] !== $data['new_password_again']):
return false;
}
This ?
return (empty($data['current_password']) || empty($data['new_password']) || $data['new_password'] !== $data['new_password_again']) ? false : '';
try below one,
return(empty($data['current_password'])||empty($data['new_password'])||$data['new_password'] !== $data['new_password_again']?FALSE:'');
How can I know that the FILES does not contain an image, and adapt my code accordingly?
I want something like:
if(expression_that_check_FILES_array_isnot_empty)
{
do sth;
}
else
{
do sthElse;
}
Thanks Pavel Janicek for his answer in comment.
I used this:
if (!empty($_FILES)){
do sth;
}
else{
do sthElse;
}
Please try :
if($_FILES['file']['name']!="")
{
do sth;
}
else
{
do sthElse;
}
Try this
if(!empty($_FILES['file']['name']))
{
do sth;
}
else
{
do sthElse;
}
Can you try it?
if(!isset($_FILES["file"]["size"]) || ($_FILES["my_file_name"]["size"] == 0) || (!empty($_FILES["file"]["name"]) {
do sth; // Throw Error
} else {
do sthElse; //Upload
}
I am just wondering if there is better way to solve my situatuion:
I have 6 independent variables to check. But if any of conditions is true it shouldnt check other. Normally I would write:
if (cond1 ) {
statement
} else {
if ( cond2 ) {
statement
} else {
if (cond3) {
statement
} else {
...
}
}
}
Surely you would admit it doesnt look good or it is not easy to read although it works. Do you know any other ways to write such if statement maybe using other notation or functions (switch? while?)
Yes, you can do
if (cond1 ) {
statement
} elseif ( cond2 ) {
statement
} elseif ( cond3 ) {
statement
}
See documentation
A more stylish way:
if(cond1):
statement1
elseif(cond2):
statement2
elseif(cond3):
statement3
elseif(cond4):
statement4
elseif(cond5):
statement5
elseif(cond6):
statement6
endif;
This is how you do it with a switch():
$a = 10;
$b = 100;
switch(true){
case ($a > $b):
echo 'a is bigger than b';break;
case ($b > $a):
echo 'b is bigger than a';break;
}
if (cond1 ) {
statement
} else {
if ( cond2 ) {
statement
} else {
if (cond3) {
statement
} else {
...
}
}
}
Change to:
if (Cond1){
}elseif (cond2){
}elseif (cond3){
}
I have this if condition:
if (isset($_REQUEST['altgeraet'])) {
$Altgeraet = 'OK';
} else {
$Altgeraet = 'NOK';
}
And I want when in the SQL Table Host_alt the value "KeinAlterHost" is the
$Altgeraet = 'OK'
This is what I tried but it didn't work:
if (isset($_REQUEST['altgeraet'])
OR ($resultarray['Hostname_alt'] == "KeinAlterHost")) {
$Altgeraet = 'OK';
} else {
$Altgeraet = 'NOK';
}
So is this setup right? I used the array_key_exists
if ((isset($_REQUEST['altgeraet']) OR (array_key_exists('KeinAlterHost',$resultarray['Hostname_alt'])) {
$Altgeraet = 'OK';
} else {
$Altgeraet = 'NOK';
}
You need an pipeline for OR
So the code will be this:
if (isset($_REQUEST['altgeraet']) || ($resultarray['Hostname_alt'] == "KeinAlterHost"))
The isset isn't quit right...
if (isset($_REQUEST['altgeraet']) OR ($resultarray['Hostname_alt'] == "KeinAlterHost")) {
$Altgeraet = 'OK';
} else {
$Altgeraet = 'NOK';
}
You need to quit it earlyer:
if (isset($_REQUEST['altgeraet']) OR ($resultarray['Hostname_alt'] == "KeinAlterHost")) {
$Altgeraet = 'OK';
} else {
$Altgeraet = 'NOK';
}
Because it checks if the request isset, and the hostname isset at you're old code. by the new one, it checks if the request isset, if not check if hostname == keinalterhost and then do the stuff...
I have this if statment
if(!empty($URL) && ($safe===true)){
//lots of code
}
Is it possible to show different error messages depending on what condition failed?
For example if $URL is empty echo "URL empty";
and if $safe===false echo "GTFO";
Just add this to your code
else if(empty($URL)
{
echo "url empty";
}
else if($safe===false)
echo "Get Out"; // be polite ;)
if (empty($url))
{
echo "URL empty";
}
elseif ($safe === false)
{
echo "GTFO";
}
else
{
//lots of code
}
} else {
if($safe === false){
die("GTFO");
}
if (empty($url)){
echo "URL Empty.";
}
}
Yes; you could make use of an else if statement.
if (!empty($URL) && ($safe===true)) {
//lots of code
} else if (empty($URL)) {
// report that url is empty
} else if ($safe === false) {
// report that safe is false
}
Alternatively, you could just use an else statement to report that the if condition was false.
I propose the following solution. It will allow you to show multiple errors and set each condition only once (instead of having so many conditions and anti-conditions as other solutions proposed).
$errors = array();
if(empty($URL) {
$errors[] = 'URL empty';
}
if($safe !== true) {
$errors[] = 'GTFO';
}
if(empty($errors)) {
//lots of code
} else {
echo '<ul>';
foreach($errors as $error_message) {
echo '<li>' . $error_message . '</li>';
}
echo '</ul>';
}