Using PHP to implement conditional statements for multiple days of the week

Could this be done?

$d=date("D");
...
else if ($d=='(Thu|Fri|Sat)') {

I have managed to make it work for one day of the week.

if ($d=='Wed') {

Thank you

Answer №1

To determine if a value is in an array, you can utilize the in_array() function

if (in_array($day, array("Monday", "Tuesday", "Wednesday"))) {

}

Answer №2

To check for multiple conditions, you can utilize the OR operator:

else if ($d=='Thu' || $d=='Fri' || $d=='Sat') {

Answer №3

Is there a reason why or or || cannot be used?

else if ($d=='Thu' || $d=='Fri' || $d=='Sat') {

If you prefer not to keep it simple, then consider using preg_replace()[docs]

preg_match('^(Thu|Fri|Sat)$', $yourtext, $matches, PREG_OFFSET_CAPTURE);
if(count($matches)) {
    /// found
}

Answer №4

If you're looking for a solution, consider using in_array(). It is the optimal choice for your needs.

if(in_array($d, array('Thu', 'Fri', 'Sat'))
  // execute code if any condition is satisfied

For more specific control, you can also utilize switch and case. These tools offer greater flexibility in handling different scenarios.

$d = date('D');

switch($d) {
  case 'Thu':
  case 'Fri':
  case 'Sat':
    // perform actions for Thu, Fri, or Sat
    break;

  case 'Mon':
    // handle Monday separately
  case 'Tue':
    // manage both Monday and Tuesday
    break;
}

The first group of cases will trigger the specified code (until break) upon meeting any of the conditions.

The second set will execute the code between Mon and Tue if Mon is true, then proceed to Tue if it holds true as well.

switch and case are powerful tools that provide precise control, particularly in situations where detailed management is required.

Answer №5

$d == '(Thu|Fri|Sat)' will only match if $d matches that specific string combination.

Options to consider:

  • Utilize a regular expression:
    preg_match('/^(Thu|Fri|Sat)$/', $d)
    ,
  • Create an array of values:
    in_array($d, array( 'Thu', 'Fri', 'Sat' ))
    , or
  • Implement multiple if statements:
    if($d == 'Thu' || $d == 'Fri' || $d == 'Sat');
  • Consider using a switch/case structure

Answer №6

Give it a shot

if(in_array($day, array('Monday', 'Tuesday', 'Wednesday'))){
}

Alternatively using || operator

if($day == 'Monday' || $day == 'Tuesday' || $day == 'Wednesday')

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Is there a way for me to create a clickable link from a specific search result retrieved from a MySQL database using an AJAX

Currently, I am attempting to create an ajax dropdown search form that provides suggestions based on results from a MySQL database. The goal is for the user to be able to click on a suggestion and be redirected to the specific product. The code I am using ...

Customization of .htaccess to Control URL Using Conditions

RewriteRule ^pictures/?$ pictures.php [L] RewriteRule ^pictures/(.*)/?$ pictures.php?album=$1 [L,QSA] RewriteRule ^pictures/(.*)/(.*)/?$ pictures.php?album=$1&page=$2 [L,QSA] I am in need of having several options like: /pictures /pictures/album /p ...

The AJAX data submission did not go through as expected

I am facing an issue with two files on my site - home.php (view) and home.php (controller). In the home.php (view) file, I have a jQuery function that sends an AJAX request based on the W3 example. However, in the home.php (controller) file, the PHP variab ...

Transferring data to a recurring include file

Is there a more efficient way to pass variables in an include that is used multiple times on the same page? Below is a sample code that technically works, but I believe there might be a better approach. Any suggestions would be greatly appreciated. index. ...

What is the best method for compressing and decompressing JSON data using PHP?

Just to clarify, I am not attempting to compress in PHP but rather on the client side, and then decompress in PHP. My goal is to compress a JSON array that includes 5 base64 images and some text before sending it to my PHP API. I have experimented with l ...

The attempt to access a URL was unsuccessful due to a failure to open the stream, along

All my requests are modified to point to index.php. Have a look at my .htaccess file provided below. IndexIgnore * <IfModule mod_rewrite.c> #Options +FollowSymLinks RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-d RewriteCond % ...

Tips for converting a string to RAW data type in Oracle using PHP

Currently struggling with sending lengthy strings to Oracle stored procedure RAW variable, encountering the following error: oci_execute(): ORA-06502: PL/SQL: numeric or value error In need of assistance on converting a String to Raw datatype. ...

The button will be visible on the page only if the provided zip code is within our service area

Scenario : Our online shopping platform allows customers to place orders and input their zip code in the address section.... All order information is stored in the do_order table, with supported zip codes listed in the shippment_details table.... https: ...

Is there a way to process the output of phantom.js in PHP efficiently?

I have successfully retrieved output from a phantom.js request via the command line and it meets my expectations. Now, I am interested in invoking phantom.js from a php script and then analyzing the output for specific content. My phantom.js script appear ...

Having trouble passing arguments to my function using do_action and add_action

My code is encountering an issue where the $var1 variable is arriving empty in my function. I've tried declaring the variable inside the function and it works, but when I attempt to declare it outside the function and pass it as a parameter with do_ac ...

What is the process for uploading JSON files through PHP code?

I have been attempting to upload a JSON file onto the server of 000webhost. Following a tutorial from w3schools (https://www.w3schools.com/php/php_file_upload.asp), I ended up removing all file checks as they were blocking JSON files. Below is the code for ...

Inspecting Facebook links

Currently working on a website and interested in incorporating a feature similar to what Facebook has. I'm referring to the link inspector, but I'm not entirely sure if that's its official name. Allow me to provide an example to clarify my r ...

Encountering the error code 'ERR_EMPTY_RESPONSE' while utilizing an AJAX-powered live search feature

My website features a live AJAX search bar that retrieves records from a MySQL database. However, when users repeatedly conduct searches by modifying the search criteria, some web browsers display an error message stating 'ERR_EMPTY_RESPONSE'. ...

Exploring the significance of the static variable $_class declaration within the load_class function of CodeIgniter

While diving into the core features of CodeIgniter, I came across a query regarding the declaration of a variable. static $_classes = array(); As highlighted in this post, the purpose of this variable is to cache class objects. I'm confused because ...

What is the best way to utilize php's preg_replace function with a basic string and placeholders?

Suppose I have the text [link="*"] where * can be any value, how can I use php to replace it with <a href="*"> where * represents the same value as before? Would preg_replace be the most suitable method for achieving this task? Thank you in advance ...

The search bar is equipped with Ajax code for real-time results. In the event that no data matches the search query

If the API call returns data, then the code works as expected. However, if it returns an empty response, it should display an error log but this is not currently happening. $.ajax({ type: "POST", url: "<?php echo base_url();?>homecontroller/catego ...

Using a comma as a parameter separator is not valid

Having trouble setting up a WhatsApp button with a custom message, I wrote a JavaScript script and called it using onclick. I've tried adjusting quotation marks but nothing seems to be working. This issue might seem minor, but as a beginner in coding ...

Live search with AJAX, navigate to a different page, and showcase the findings

I currently have multiple web pages that all feature the same search form. What I want to achieve is for the results page to load dynamically as the user starts typing, replacing the current page and displaying all relevant items found. How can I implement ...

What issues could arise from utilizing PHP for generating variables within my CSS file?

One major limitation of CSS is the absence of variables. It would be beneficial to have the ability to use variables for controlling things like the location of imported CSS and color schemes within a design. An alternative solution could involve using a ...

Encountering an issue with Laravel 5.1 migration: primary key auto increment error

I have been studying Laravel for some time now and have created some basic projects for myself. Today, I tried to migrate a table with multiple integer fields, but encountered an error. Each integer field is set to auto_increment and primary, which may be ...