PHP Operators পর্ব -৬ : PHP Ternary and Null Coalescing Operator

PHP তে Ternery Operator কি ?

Ternary Operator

অক্সফোর্ড ইংরেজি অভিধান অনুযায়ী Ternary শব্দের অর্থ হচ্ছে “তিনটি অংশে গঠিত”। নামের মতোই PHP তে Ternary Operator টি হচ্ছে একটি Conditional Operator যা তিনটি অংশে গঠিত , আর তা হচ্ছে: Condition ? Expression 1 : Expression 2. যদি condition true হয় , তাহলে Expression 1 রিটার্ন করবে অন্যথায় Expression 2 রিটার্ন করবে। Ternary Operator টি অনেকটা if….else স্টেটমেন্ট এর মতোই। চলুন নিচের উদাহরণ দুটি দেখা যাক :

PHP Ternary and Null Coalescing Operator

উদাহরণ ১: Ternary Operator ছাড়া if…else দিয়ে condition চেক

<?php
$age = 19; //input by user

if($age > 17){
    echo "Yes you are eligible";
}else{
    echo  "Sorry, not eligible";
}
?>

Zend Certified PHP Engineering (ZCPE) Course

উদাহরণ ২: Ternary Operator দিয়ে condition চেক

<?php
$age=19;
echo ($age>17)?"Yes you are eligible":"Sorry, not eligible";
?>

উদাহরণ ৩: Ternary অপারেটর ছাড়া if…else দিয়ে variable এ Default value assign:

<?php
if (empty($_POST['action'])) {
    $action = 'default';
} else {
    $action = $_POST['action'];
}
?>
?>

উদাহরণ ৪: Ternary অপারেটর দিয়ে variable এ Default value assign:

<?php
$action = (empty($_POST['action'])) ? 'default' : $_POST['action'];
?>

Zend Certified PHP Engineering (ZCPE) Course

PHP তে কি Nested Ternary Operator লেখা যায়:

হ্যাঁ লেখা যায়, তবে Nested Condition এর ক্ষেত্রে if ..elseif..n…else অথবা switch case ব্যবহার করা ই ভালো।

উদাহরণ ৫: Nested if…else :

<?php
$bTest1 = false;
$bTest2 = false;

	if ($bTest1) {
		if ($bTest2) {
			echo 'test 1 true, test 2 true';
		}
		else {
			echo 'test 1 true, test 2 false';
		}
	}
	else {
		if ($bTest2) {
			echo 'test 1 false, test 2 true';
		}
		else {
			echo 'test 1 false, test 2 false';
		}
	}
?>

ব্যাখ্যা :এখানে $bTest1 এর ডিফল্ট value false হওয়াতে, প্রথম if condition কাজ করবেনা এবং প্রোগ্রাম তখন else statement এ চলে যাবে। আবার else statement এ প্রথম if condition টি false হওয়ায় প্রোগ্রাম আবার else statement এ যাবে এবং ফলাফল প্রিন্ট করবে : test 1 false, test 2 false

উপরের কাজটি আমরা nested ternery operator দিয়েও করতে পারি।

উদাহরণ ৬: Nested Ternary Operator:

<?php
$bTest1 = false;
$bTest2 = false;
echo $bTest1?($bTest2?'test 1 true, test 2 true':'test 1 true, test 2 false'):($bTest2?'test 1 false, test 2 true':'test 1 false, test 2 false');
?>

Zend Certified PHP Engineering (ZCPE) Course

PHP তে Null Coalescing Operator কি ?

Null Coalescing Operator

PHP-7 থেকে যেকোনো Null বা ফাঁকা Variable এ নতুন value assign বা মান রাখার জন্য Null Coalescing Operator টি ব্যবহৃত হয়। নিচের উদাহরণ লক্ষ্য করুন :

উদাহরণ ১: Null Coalescing Operator ছাড়া if…else দিয়ে নতুন value assign

<?php
if (isset($_POST['action'])) {
    $action = $_POST['action'];
} else {
    $action = 'default';
}
?>

উদাহরণ ২: Null Coalescing Operator দিয়ে নতুন value assign

<?php
// Example usage for: Null Coalesce Operator
$action = $_POST['action'] ?? 'default';
?>

উদাহরণ ৩: Nesting null coalescing operator

<?php

$foo = null;
$bar = null;
$baz = 1;
$qux = 2;

echo $foo ?? $bar ?? $baz ?? $qux; // outputs 1

?>

PHP Ternary and Null Coalescing Operator

আমি মাসুদ আলম, বাংলাদেশের ৩৬ তম Zend Certified Engineer । ২০০৯ সালে কম্পিউটার সাইন্স থেকে বেচেলর ডিগ্রী অর্জন করি। দীর্ঘ ১৫ বছর আমি Winux Soft, SSL Wireless, IBCS-PRIMAX, Max Group, Canadian International Development Agency (CIDA), Care Bangladesh, World Vision, Hellen Keller, Amarbebsha Ltd সহ বিভিন্ন দেশি বিদেশী কোম্পানিতে ডেটা সাইন্স, মেশিন লার্নিং, বিগ ডেটা, ওয়েব ডেভেলপমেন্ট এবং সফটওয়্যার ডেভেলপমেন্ট এর উপর বিভিন্ন লিডিং পজিশন এ চাকরি এবং প্রজেক্ট লিড করি। এছাড়াও বাংলাদেশের ১৮৫ জন জেন্ড সার্টিফাইড ইঞ্জিনিয়ার এর মধ্যে ১২০ এরও অধিক ছাত্র আমার হাতে জেন্ড সার্টিফাইড ইঞ্জিনিয়ার হয়েছেন। বর্তমানে w3programmers ট্রেনিং ইনস্টিটিউট এ PHP এর উপর Professional এবং Advance Zend Certified PHP -8.2 Engineering, Laravel Mastering Course with ReactJS, Python Beginning To Advance with Blockchain, Machine Learning and Data Science, Professional WordPress Plugin Development Beginning to Advance কোর্স করাই। আর অবসর সময়ে w3programmers.com এ ওয়েব টেকনোলজি নিয়ে লেখালেখি করি।

Leave a Reply