Jump to content

creating functions


hyster

Recommended Posts

to learn a little about creating functions i decided to do a geetings message depending on the time of day

which works fine.

 

function greet()
{
    $date=date("H");

      if ($date >= '00' && $date <'12'){
      echo "Good Morning";
      }elseif ($date >= '12' && $date <'18') {
      echo "Good Afternoon" ;
      }elseif ($date >= '18' && $date <'24') {
      echo "Good Evening" ;
      }else{
      echo "All Messed Up" ;
      }
}
echo "hello ";
greet()

 

so what i want to do next and the part im stuck on is using a switch to change language. the code below dosent work but i hope u get an idea of what i want

function greet()
{
    $date=date("H");

    $uk =       if ($date >= '00' && $date <'12'){
                echo "Good Morning";
                }elseif ($date >= '12' && $date <'18') {
                echo "Good Afternoon" ;
                }elseif ($date >= '18' && $date <'24') {
                echo "Good Evening" ;
                }else{
                echo "All Messed Up" ;
                }
           
     $pl =    if ($date >= '00' && $date <'12'){
              echo "Dzien dobry";
              }elseif ($date >= '12' && $date <'18') {
              echo "Dzien dobry" ;
              }elseif ($date >= '18' && $date <'24') {
              echo "Dobry wieczor" ;
              }else{
              echo "Zjebales" ;
              }
    
}
echo "hello";
greet(pl)

Link to comment
https://forums.phpfreaks.com/topic/237410-creating-functions/
Share on other sites

I changed it a bit.

 

<?php
function greet($language) {
    $date=date("H");
if($language == "uk") {
if ($date >= '00' && $date < '12'){
                return "Good Morning";
                }elseif ($date >= '12' && $date < '18') {
                return "Good Afternoon";
                }elseif ($date >= '18' && $date < '24') {
                return "Good Evening";
                }else{
                return "All Messed Up";
                }
} elseif ($language == "pl") {           
if ($date >= '00' && $date < '12'){
              return "Dzien dobry";
              }elseif ($date >= '12' && $date < '18') {
              return "Dzien dobry" ;
              }elseif ($date >= '18' && $date < '24') {
              return "Dobry wieczor" ;
              }else{
              return "Zjebales" ;
              }
} else {
return "No Language Selected";
}

}

echo greet(pl);
echo "<br />";
echo greet(uk);
?>

Link to comment
https://forums.phpfreaks.com/topic/237410-creating-functions/#findComment-1219902
Share on other sites

thanks m8. i understand that so far.

now :)

if i wanted to add another 'section' for the day so i can do greet("pl/d") how would i add that ???

 

function greet($language,$date1) {

// the other code from above//

        $date1 = date('D');
        if ($date1 == 'Mon'){
        return "Nom";}
        elseif ($date1 == 'Tue'){
        return "Eut";
        }elseif ($date1 == 'Wed'){
        return "Dew";
        }elseif ($date1 == 'Thu'){
        return "Uht";
        }elseif ($date1 == 'Fri'){
        return "Irf";
        }

echo greet('pl/d');
echo "<br />";
echo greet("uk/d");

Link to comment
https://forums.phpfreaks.com/topic/237410-creating-functions/#findComment-1219922
Share on other sites

Well the standard way would be to have them as variables

 

//variable values coming from somewhere
$language = "pl";
$month = date('M');
$day = date('D');
$year = date('Y');
$hour = date('h');
$minute = date('i');
$second = date('s');

greet($language,$month,$day,$year,$hour,$minute,$second);

in function follows same order

 

And in function you wouldn't need to change the value because it comes from before it

function greet($language,$date1) {
//$date1 = date('D'); //not needed $date1 value already there (unless you want to change the value)

 

You can also do arrays or a string and can explode them separated by any delimiter that makes sense to you.

Checking if value exists or is not empty as well.

 

I don't have time to explain fully, I need to go to work.

Hopefully I explained well enough or others can explain and give more examples of arrays and even exploding arrays from strings of text using delimiters

Link to comment
https://forums.phpfreaks.com/topic/237410-creating-functions/#findComment-1220033
Share on other sites

i understood the top piece of code i think but its not what im after as such.

the content im using is not relevent to what i want to achive. its just filler content that shows something diffrent.

 

im trying to figure out how to get 2 (or more) blocks of code to use arguments inside of a function.

the

 

ie:

 

function test($var1,$var2,$var$){

$var1 block of code{

qq

}

$var2 block of code{

ee

}

$var3 block of code{

rr

}

}

 

text (qq/ee/rr)

hope that makes more sense to what im trying to learn

 

 

Link to comment
https://forums.phpfreaks.com/topic/237410-creating-functions/#findComment-1220121
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.