skip to Main Content

It is possible to put If inside $message variable? code shown below :

$message =   "Semangat Pagi Akang & Teteh Semua!n"
        . "Nyai Iteung mengucapkan selamat ulang tahun &#128172 untuk:nn" 
        if($getNowCuti->AllKaryawan->jenis_kelamin == 'L')
        {
            . "Pak" . $stringKaryawan . "nn"
        } else {
            . "Bu" . $stringKaryawan . "nn"
        }
        . "Selamat cuti juga yaa untuk:nn"
        if($getNowCuti->AllKaryawan->jenis_kelamin == 'L')
        {
            . "Pak" . $stringKaryawan . "nn"
        } else {
            . "Bu" . $stringKaryawan . "nn"
        }
        . "Harap rekan-rekan Kabayan Group tidak memberikan tugas kepada pegawai diatas pada hari ini.";

It shows an error, how should I fixed it? Thanks guys, have a great day!

3

Answers


  1. If you want to do this in one line, try like this.

    $message = "Semangat Pagi Akang & Teteh Semua!n"
    . "Nyai Iteung mengucapkan selamat ulang tahun &#128172 untuk:nn"
    . $stringKaryawan . "nn"
    . "Selamat ...."
    . ($getNowCuti->AllKaryawan->jenis_kelamin == 'L' ? "Pak" . $stringKaryawan . "nn" : "Bu" . $stringKaryawan . "nn")
    . ".............";
    

    Otherwise, the following method (answer by @Gert B.) is a nicer use case:

    $message = "First partn"
    . "second partn"; 
    if($condition) {
        $message .= "if partn";
    } else {
        $message .= "else partn";
    }
    $message .= "anything after the if statementn";
    
    Login or Signup to reply.
  2. You cannot add a if directly inside of the build op the string, but you can append to the string inside of an if statement:

    $message =   "Semangat Pagi Akang & Teteh Semua!n"
        . "Nyai Iteung mengucapkan selamat ulang tahun &#128172 untuk:nn";
        if($getNowCuti->AllKaryawan->jenis_kelamin == 'L')
        {
            $message .= "Pak" . $stringKaryawan . "nn";
        } else {
            $message .= "Bu" . $stringKaryawan . "nn";
        }
        $message .= "Selamat cuti juga yaa untuk:nn";
        if($getNowCuti->AllKaryawan->jenis_kelamin == 'L')
        {
           $message .= "Pak" . $stringKaryawan . "nn";
        } else {
            $message .= "Bu" . $stringKaryawan . "nn";
        }
        $message .= "Harap rekan-rekan Kabayan Group tidak memberikan tugas kepada pegawai diatas pada hari ini.";
    
    Login or Signup to reply.
  3. you can make it even easier to read by assigning a variable ahead since you use it twice.

    $kelamin = $getNowCuti->AllKaryawan->jenis_kelamin == 'L' ? 'Pak' : 'Bu';
    
    $message = 
        "Semangat Pagi Akang & Teteh Semua!n" .
        "Nyai Iteung mengucapkan selamat ulang tahun &#128172 untuk:nn" .
        "$kelamin $stringKaryawan nn" .
        "Selamat cuti juga yaa untuk:nn" .
        "$kelamin $stringKaryawan nn"
        "Harap rekan-rekan Kabayan Group tidak memberikan tugas kepada pegawai diatas pada hari ini.";
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search