Jump to content

if function


chuddyuk

Recommended Posts

Im trying to replace data from the database with different words:
[code]
if ($collection = 'ethnicchic'){
echo "Ethnic Chic";
}
elseif ($collection = 'indiansummer'){
echo "Indian Summer";
}
elseif ($collection = 'africaninspirations'){
echo "African Inspirations";
}
elseif ($collection = 'contemporarycreations'){
echo "Contemporary Creations";
}
[/code]
i am using the above code to try and change whats in the databse to something else, in this case to put a space betweeen my letters. but it doesnt change anything. any ideas?
Link to comment
https://forums.phpfreaks.com/topic/11011-if-function/
Share on other sites

== is the comparison operator. Change your code to this:
[code]
if ($collection == 'ethnicchic'){
echo "Ethnic Chic";
}
elseif ($collection == 'indiansummer'){
echo "Indian Summer";
}
elseif ($collection == 'africaninspirations'){
echo "African Inspirations";
}
elseif ($collection == 'contemporarycreations'){
echo "Contemporary Creations";
}[/code]
Link to comment
https://forums.phpfreaks.com/topic/11011-if-function/#findComment-41132
Share on other sites

You don't need to use the "elseif" in this instance, since you could write this as four individual "if" statements:
[code]<?php
if ($collection == 'ethnicchic') echo "Ethnic Chic";
if ($collection == 'indiansummer') echo "Indian Summer";
if ($collection == 'africaninspirations') echo "African Inspirations";
if ($collection == 'contemporarycreations') echo "Contemporary Creations";
?>[/code]
You can also use a "switch" statement here:
[code]<?php
switch ($collection) {
    case 'ethnicchic':
         $tmp = 'ethnic chic';
         break;
    case 'indiansummer':
         $tmp = 'indian summer';
         break;
    case 'africaninspirations':
         $tmp = 'african inspirations';
         break;
     case 'contemporarycreations':
         $tmp = 'contemporary creations';
         break;
}
echo ucwords($tmp);
?>[/code]

Just giving you other options.

Ken
Link to comment
https://forums.phpfreaks.com/topic/11011-if-function/#findComment-41136
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.