Skip to content
View in the app

A better way to browse. Learn more.

Web Designer Forum

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

if else

Featured Replies

Hi,

I have this variable $invoice it can start with A Like A74675 or B Like B39947 or C.

I want the $item_name to be red if the $invoice started with A and I want it to be blue if the $invoice is started with B and at the end to be black if the $invoice started with C.

 

 

Is the there any way to achieve that?

 

Youssef

I'm not great with PHP so there is a very good chance this is the wholly wrong way of going about it or it may not even work. but

 



if(strstr($invoice,'A') === TRUE)
{
//Do something with A 
}
elseif(strstr($invoice,'B') === TRUE)
{
//Do something with B
}
elseif(strstr($invoice,'C') === TRUE)
{
//Do something with C
}
else {
// Do nothing
} 

 

Again, PHP really isn't my thing so don't expect it to work but it's always worth a bash, right? =)

 

I think strstr doesn't require perfect match and I think it starts at the beginning (IE won't just match any A in any string) but I'm not certain on that.

Edited by Glowbridge

  • Author

I'm not great with PHP so there is a very good chance this is the wholly wrong way of going about it or it may not even work. but

 



if(strstr($invoice,'A') === TRUE)
{
//Do something with A 
}
elseif(strstr($invoice,'B') === TRUE)
{
//Do something with B
}
elseif(strstr($invoice,'C') === TRUE)
{
//Do something with C
}
else {
// Do nothing
} 

 

Again, PHP really isn't my thing so don't expect it to work but it's always worth a bash, right? =)

 

I think strstr doesn't require perfect match and I think it starts at the beginning (IE won't just match any A in any string) but I'm not certain on that.

 

Thank you this worked perfect .

The PHP switch statement is good for comparing something against a series of values...

 

<?php
 $i=$invoice[0];
 switch ($i) {
    case "A":           
         $colour="red"; 
         break;     
    case "B": 
         $colour="blue";       
         break;     
    default:      
         $colour="black"; 
    }
?>

 

...then if $item_name holds the actual text you want to output, you could do something like...

 

echo "<span style=\"color:$colour\">$item_name</span>";

The PHP switch statement is good for comparing something against a series of values...

 

<?php
 $i=$invoice[0];
 switch ($i) {
    case "A":           
         $colour="red"; 
         break;     
    case "B": 
         $colour="blue";       
         break;     
    default:      
         $colour="black"; 
    }
?>

 

...then if $item_name holds the actual text you want to output, you could do something like...

 

echo "<span style=\"color:$colour\">$item_name</span>";

 

i was actually gonna suggest this method which is the most logical way to do it

  • Author

The PHP switch statement is good for comparing something against a series of values...

 

<?php
 $i=$invoice[0];
 switch ($i) {
    case "A":           
         $colour="red"; 
         break;     
    case "B": 
         $colour="blue";       
         break;     
    default:      
         $colour="black"; 
    }
?>

 

...then if $item_name holds the actual text you want to output, you could do something like...

 

echo "<span style=\"color:$colour\">$item_name</span>";

 

This is a quite a good way to do it, thank you.

\o/ I'm not completely PHP dense. Ok so there is a better way of doing it, but my way didn't make the computer catch fire so I'm happy =)

Edited by Glowbridge

  • Author

\o/ I'm not completely PHP dense. Ok so there is a better way of doing it, but my way didn't make the computer catch fire so I'm happy =)

Hehehehe I am still using your way :) I am lazy to change it now, so I will keep using it for a while.

Create an account or sign in to comment

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.