Matt C. Well-known member Dec 7, 2018 #1 What's the best way to condense this piece of code? Is there a way to put them all together? PHP: if ($nick === null) { $nick = 'null'; } if ($role === null) { $role = 'null'; } if ($topicUrl === null) { $topicUrl = 'null'; } Thank you!
What's the best way to condense this piece of code? Is there a way to put them all together? PHP: if ($nick === null) { $nick = 'null'; } if ($role === null) { $role = 'null'; } if ($topicUrl === null) { $topicUrl = 'null'; } Thank you!
K Kirby Well-known member Dec 7, 2018 #2 Don't think it is the best way, but one way: PHP: $vars = ['nick', 'role', 'topicUrl']; foreach ($vars as $var) { if ($$var == null) $$var = 'null'; } Or even shorter with PHP 7+ PHP: $vars = ['nick', 'role', 'topicUrl']; foreach ($vars as $var) $$var = $$var ?? 'null'; Last edited: Dec 7, 2018
Don't think it is the best way, but one way: PHP: $vars = ['nick', 'role', 'topicUrl']; foreach ($vars as $var) { if ($$var == null) $$var = 'null'; } Or even shorter with PHP 7+ PHP: $vars = ['nick', 'role', 'topicUrl']; foreach ($vars as $var) $$var = $$var ?? 'null';
Matt C. Well-known member Dec 7, 2018 #3 Kirby said: Don't think it is the best way, but one way: PHP: $vars = ['nick', 'role', 'topicUrl']; foreach ($vars as $var) { if ($$var == null) $$var = 'null'; } Click to expand... Thank you Kirby. When I run the cron entry, I get an array to string conversion error Code: $nullVars = ['nick', 'role', 'topicUrl']; foreach ($nullVars as $nullVar) { if ($$nullVars == null) $$nullVar = 'null'; }
Kirby said: Don't think it is the best way, but one way: PHP: $vars = ['nick', 'role', 'topicUrl']; foreach ($vars as $var) { if ($$var == null) $$var = 'null'; } Click to expand... Thank you Kirby. When I run the cron entry, I get an array to string conversion error Code: $nullVars = ['nick', 'role', 'topicUrl']; foreach ($nullVars as $nullVar) { if ($$nullVars == null) $$nullVar = 'null'; }