Conditional switch statements in PHP
I think you're searching for something like this (this is not exactly what you want or at least what I understand is your need).
switch (true)
finds the cases which evaluate to a truthy value, and execute the code within until the first break; it encounters.
<?php
switch (true) {
case ($totaltime <= 1):
echo "That was fast!";
break;
case ($totaltime <= 5):
echo "Not fast!";
break;
case ($totaltime <= 10):
echo "That's slooooow";
break;
}
?>
PHP- Switch case statement with conditional switch
Switch only checks if the first condition is equal to the second, this way:
switch (CONDITION) {
case CONDITION2:
echo "CONDITION is equal to CONDITION2";
break;
}
So you have to do it this way:
switch (true) {
case $totaltime <= 1: #This checks if true (first condition) is equal to $totaltime <= 1 (second condition), so if $totaltime is <= 1 (true), is the same as checking true == true.
echo "That was fast!";
break;
case $totaltime <= 5:
echo "Not fast!";
break;
case $totaltime >= 10 && $totaltime<=13:
echo "That's slooooow";
break;
}
Instead of this i'll go for if-elseif
statements. Is easier to understand at first sight:
if ($totaltime <= 1) {
echo "That was fast!";
} elseif($totaltime <= 5) {
echo "Not fast!";
} elseif($totaltime >= 10 && $totaltime<=13) {
echo "That's slooooow";
}
PHP switch case more than one value in the case
The simplest and probably the best way performance-wise would be:
switch ($var2) {
case 1:
case 2:
$var3 = 'Weekly';
break;
case 3:
$var3 = 'Monthly';
break;
case 4:
case 5:
$var3 = 'Quarterly';
break;
}
Also, possible for more complex situations:
switch ($var2) {
case ($var2 == 1 || $var2 == 2):
$var3 = 'Weekly';
break;
case 3:
$var3 = 'Monthly';
break;
case ($var2 == 4 || $var2 == 5):
$var3 = 'Quarterly';
break;
}
In this scenario, $var2 must be set and can not be null or 0
How to get PHP switch case conditional statements code work properly on wordpress function.php
function todays_content(){
$date = date('l');
switch ($date) {
case 'Sunday': $content = do_shortcode('[sunday_content]'); break;
case 'Monday': $content = do_shortcode('[monday_content]'); break;
case 'Tuesday': $content = do_shortcode('[tuesday_content]'); break;
case 'Wednesday': $content = do_shortcode('[wednesday_content]'); break;
case 'Thursday': $content = do_shortcode('[thursday_content]'); break;
case 'Friday': $content = do_shortcode('[friday_content]'); break;
case 'Saturday': $content = do_shortcode('[saturday_content]'); break;
}
return '<div class="daily_contant">'.$content.'</div>';
}
add_shortcode( 'show_today_content', 'todays_content' );
How can i avoid conditional statements or/and switch statements?
To avoid using conditionals you could use some sort of lookup to match the type to the field display I think.
Something like this perhaps:
$lookup = [
"DVD" => ["field" => "size", "append" => "MB"],
"Furniture" => ["field" => "dimentions", "append" => ""],
"Book" => ["field" => "weight", "append" => "Kg"],
];
$LookupItem = $lookup[$product["product_type"]];
echo $product[$LookupItem["field"]].$LookupItem["append"];
Live demo: https://3v4l.org/Kc4hV
SWITCH-statement two case with same condition and also different condition
For any variables that contain the same value why use a switch at all? Just define those variables before the switch statement. Use the switch statement only for variables that contains different values.
EDIT
In that case there is no reason why you can't use 2 switch statements, like this:
switch(strtolower($name))
{
case "dog":
$pic = "/images/itscute.jpg";
break;
case "cat":
$pic = "/images/cat.jpg";
break;
case "bird":
$pic = "/images/bird.jpg";
break;
}
switch(strtolower($name))
{
case "dog":
case "cat":
$info = "four legs";
break;
case "bird":
$info = "two legs";
break;
}
I do recommend that you use strtolower(), like my example shows, to avoid any case problems. You can use any number of switch statements as you need.
As for turning "dog" into "Dog's" just add the "'s" to the variable, like this:$name = ucwords($name . "'s");
Related Topics
Check If a Remote Page Exists Using PHP
JSON_Encode or Remove Last Comma
Isset() Function Is Returning True Even When Item Is Not Set
How to Add PHP Pagination in Array'S
Displaying Multiple Lines of a File, Never Repeating
Symfony How to Return All Logged in Active Users
Is This a PHP Date() Bug, or Is There Something Wrong with My Code
Automatically Trimming an Mp3 in PHP
Wordwrap/Cut Text in HTML String
Location for Session Files in Apache/Php
Importing Multiple CSV Files to MySQL Tables
Best Practice for Working with Currency Values in PHP
Check If a "Run-Time" Multidimensional Array Key Exists