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
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
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
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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.