AmitDiwan has Published 10744 Articles

If elseif else or ternary operator to compare numbers PHP?

AmitDiwan

AmitDiwan

Updated on 12-Oct-2020 14:02:03

407 Views

You can use any of them, but a more professional approach is using the ternary operator (?). Performance of both if-else and ternary operator is the same.Let’s say we have the following two variable values −$value1=10; $value2=0;We need to compare the above 2 variable values.Example Live Demo ... Read More

How can I count true and false values in my PHP array?

AmitDiwan

AmitDiwan

Updated on 12-Oct-2020 13:56:40

910 Views

Let’s say the following is our array −$isMarriedDetails = [    false,    true,    false,    true,    true,    false,    false,    true,    false ];To count true and false values from an array, at first, count total values and subtract the number of true results. In ... Read More

Return an array with numeric keys PHP?

AmitDiwan

AmitDiwan

Updated on 12-Oct-2020 13:50:55

211 Views

For this, use foreach loop along with [] and implement arary_values() method.Example Live Demo OutputArray ( [0] => Array ( [0] => Array ( [LASTNAME] => Doe [FIRSTNAME] => John ) ) [1] => Array ( [0] => Array ( [LASTNAME] => Miller [FIRSTNAME] => David ) ) )

PHP array_push() to create an associative array?

AmitDiwan

AmitDiwan

Updated on 12-Oct-2020 13:47:40

1K+ Views

To create associative arrays in PHP, use [] brackets. You don't need to use array_push().Example Live Demo OutputArray ( [0] => Array ( [emp_id] => 101 [emp_first_name] => John [emp_last_name] => Doe [emp_country_name] => AUS ) )

Can we define constant in class constructor PHP?

AmitDiwan

AmitDiwan

Updated on 12-Oct-2020 13:43:11

822 Views

No, you cannot define constant in class constructor, you can use constant at the class level.Let’s say we have the following class −class ConstantDemo { }Define a constant in the class −class ConstantDemo {     const LANGUAGE_NAME="PHP"; } Example Live Demo OutputThe language Name is=PHP

Reference assignment operator in PHP to assign a reference?

AmitDiwan

AmitDiwan

Updated on 12-Oct-2020 13:41:24

329 Views

Let’s say we have the following value −$nextValue=100;Let us take a new variable and assign a reference −$currentValue = &$nextValue;To assign a reference, use the reference assignment operator i.e. =&$anyVariableName in PHP.The PHP code is as follows −Example Live Demo OutputNext Value=100 Current Value=100 After changing the ... Read More

PHP print keys from an object?

AmitDiwan

AmitDiwan

Updated on 12-Oct-2020 13:35:45

6K+ Views

Let’s say the following is our object −$employeeDetails = (object) [     'firstName' => 'John',     'lastName' => 'Doe',     'countryName' => 'US' ];We want the following output i.e. only the keys −firstName lastName countryNameTo display only the keys from an object, use array_keys() in PHP.Example Live Demo ... Read More

Convert timestamp coming from SQL database to String PHP?

AmitDiwan

AmitDiwan

Updated on 12-Oct-2020 13:26:15

468 Views

To convert timestamp to string, use setTimestamp(). Let’s say the following is our input i.e. timestamp −$SQLTimestamp = 1600320600000;We want the following output i.e. Date string −2020-09-17 05:30:00At first, get seconds from Timestamp −$seconds = round($SQLTimestamp/1000, 0);Example Live Demo Output2020-09-17 05:30:00

How to build dynamic associative array from simple array in php?

AmitDiwan

AmitDiwan

Updated on 12-Oct-2020 13:20:17

2K+ Views

Let’s say we have the following array −$namesArray = ['John', 'Adam', 'Robert'];We want the following output i.e. an associative array from the above array −Array ( [John] => Array ( [Adam] => Array ( [Robert] => Smith ) ) )Example Live Demo OutputArray ( [John] => Array ( [Adam] => Array ( [Robert] => Smith ) ) ) Doe

How to insert into a table all combination of values in PHP arrays?

AmitDiwan

AmitDiwan

Updated on 12-Oct-2020 13:14:21

1K+ Views

For this, use the foreach loop and insert into statement in order to insert all combinations of values in PHP arrays.Let’s say we have the following arrays −$name1 = ["John", "Mike"]; $name2 = ["David", "Sam"]; $name3 = ["Bob", "Adam"];Example Live Demo

Advertisements