Found 1060 Articles for PHP

If elseif else or ternary operator to compare numbers PHP?

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

405 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 This will produce the following outputOutputThe value is=550000

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

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

906 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 this way, you will get the number of false values and same for true.Example Live Demo OutputThis will produce the following outputNumber of false value=5 Number of true value=4

Return an array with numeric keys PHP?

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

210 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
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
Updated on 12-Oct-2020 13:43:11

819 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
Updated on 12-Oct-2020 13:41:24

326 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 value of next will reflect the current value because of => Next Value=45000 Current Value=45000

PHP print keys from an object?

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 OutputAll Keys are as follows= firstName lastName countryName

Convert timestamp coming from SQL database to String PHP?

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

466 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
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
Updated on 12-Oct-2020 13:14:21

998 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