Home > database >  How can I put if inside $message variable
How can I put if inside $message variable

Time:10-31

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:\n\n" 
        if($getNowCuti->AllKaryawan->jenis_kelamin == 'L')
        {
            . "Pak" . $stringKaryawan . "\n\n"
        } else {
            . "Bu" . $stringKaryawan . "\n\n"
        }
        . "Selamat cuti juga yaa untuk:\n\n"
        if($getNowCuti->AllKaryawan->jenis_kelamin == 'L')
        {
            . "Pak" . $stringKaryawan . "\n\n"
        } else {
            . "Bu" . $stringKaryawan . "\n\n"
        }
        . "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!

CodePudding user response:

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:\n\n"
. $stringKaryawan . "\n\n"
. "Selamat ...."
. ($getNowCuti->AllKaryawan->jenis_kelamin == 'L' ? "Pak" . $stringKaryawan . "\n\n" : "Bu" . $stringKaryawan . "\n\n")
. ".............";

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

$message = "First part\n"
. "second part\n"; 
if($condition) {
    $message .= "if part\n";
} else {
    $message .= "else part\n";
}
$message .= "anything after the if statement\n";

CodePudding user response:

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:\n\n";
    if($getNowCuti->AllKaryawan->jenis_kelamin == 'L')
    {
        $message .= "Pak" . $stringKaryawan . "\n\n";
    } else {
        $message .= "Bu" . $stringKaryawan . "\n\n";
    }
    $message .= "Selamat cuti juga yaa untuk:\n\n";
    if($getNowCuti->AllKaryawan->jenis_kelamin == 'L')
    {
       $message .= "Pak" . $stringKaryawan . "\n\n";
    } else {
        $message .= "Bu" . $stringKaryawan . "\n\n";
    }
    $message .= "Harap rekan-rekan Kabayan Group tidak memberikan tugas kepada pegawai diatas pada hari ini.";

CodePudding user response:

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:\n\n" .
    "$kelamin $stringKaryawan \n\n" .
    "Selamat cuti juga yaa untuk:\n\n" .
    "$kelamin $stringKaryawan \n\n"
    "Harap rekan-rekan Kabayan Group tidak memberikan tugas kepada pegawai diatas pada hari ini.";
  • Related