Use Ternary Operator in PHP as Shorthand for If-Else

AmitDiwan
Updated on 13-Oct-2020 06:56:46

351 Views

The syntax is as follows −$anyVariableName=yourCondition ? if condition becomes true then this will be executed: if condition becomes false then this gets executed;ExampleThe PHP code is as follows Live Demo OutputThis will produce the following outputGreater Than or Equal to 100

Add Backslash Inside Array of Strings in PHP

AmitDiwan
Updated on 13-Oct-2020 06:52:24

588 Views

To add backslash inside array of strings, use json_encode().Let’s say the following is our array −$value = [    "ADD", "REMOVE","SELECT","MODIFY" ];We want the output with backslash inside array of strings i.e −"[\"ADD\",\"REMOVE\",\"SELECT\",\"MODIFY\"]"ExampleThe PHP code is as follows − Live Demo OutputThis will produce the following output"[\"ADD\",\"REMOVE\",\"SELECT\",\"MODIFY\"]"

Remove Extra Whitespace from the Beginning in PHP

AmitDiwan
Updated on 13-Oct-2020 06:48:58

185 Views

If you want to remove extra whitespace from the beginning, use ltrim() in PHP. Let’s say the following is our variable −$value="  Hello, welcome to PHP Tutorial  ";We want the output with no whitespace on the left −Hello, welcome to PHP TutorialExample Live Demo OutputThis will produce the following outputThe actual value is= Hello, welcome to PHP Tutorial The actual length is=34 After removing extra whitespace from beginning=32 The result is=Hello, welcome to PHP Tutorial

Change Date Format in PHP

AmitDiwan
Updated on 13-Oct-2020 06:45:20

143 Views

Yes, change the date format in PHP using the date() along with strtotime(). Let’s say the following is our date −$dateValue = "2020-09-19";Change the date format using the strtotime() method −$modifiedDate= date("d-m-Y", strtotime($dateValue));  Example Live Demo OutputThis will produce the following outputThe original date format is=2020-09-19 The changed date format is= 19-09-2020

If, Elseif, Else or Ternary Operator to Compare Numbers in PHP

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

Count True and False Values in PHP Array

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

912 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 in PHP

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
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 ) )

Define Constant in Class Constructor in PHP

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

824 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

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

330 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

Advertisements