Server Side Programming Articles

Page 1016 of 2109

How do I use the ternary operator ( ? : ) in PHP as a shorthand for "if / else"?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 427 Views

The ternary operator (? :) in PHP provides a compact way to write simple if-else statements in a single line. It evaluates a condition and returns one of two values based on whether the condition is true or false. Syntax The basic syntax of the ternary operator is − $variable = condition ? value_if_true : value_if_false; Basic Example Here's how to use the ternary operator to check if a value meets a condition ? Greater Than or Equal to 100 Comparison with If-Else The ...

Read More

PHP How to add backslash inside array of strings?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 640 Views

To add backslashes inside an array of strings, you can use json_encode() function twice. This technique is useful when you need to escape quotes within JSON strings for JavaScript or database storage. Let's say the following is our array − $value = [ "ADD", "REMOVE", "SELECT", "MODIFY" ]; We want the output with backslashes inside array of strings i.e − "["ADD", "REMOVE", "SELECT", "MODIFY"]" Using Double json_encode() The PHP code is as follows − This will produce the following output − ...

Read More

If elseif else or ternary operator to compare numbers PHP?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 478 Views

In PHP, you can compare numbers using if-elseif-else statements or the ternary operator (?:). Both have identical performance, but the ternary operator provides a more concise syntax for simple comparisons. Using if-elseif-else Statement The traditional approach uses conditional statements to compare numbers ? Value1 (10) is less than Value2 (20) Using Ternary Operator The ternary operator provides a compact way to write simple conditional statements ? 15 is greater Nested Ternary for Multiple Comparisons You can chain ternary operators for ...

Read More

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

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 993 Views

In PHP, you can count true and false values in an array using array_filter() combined with count(). The array_filter() function removes falsy values by default, making it perfect for counting true values. Syntax $trueCount = count(array_filter($array)); $falseCount = count($array) - $trueCount; Example Let's count true and false values from a boolean array − Number of false values: 5 Number of true values: 4 How It Works The array_filter() function removes all falsy values (false, 0, null, empty string) from the array. When we count the ...

Read More

Return an array with numeric keys PHP?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 266 Views

In PHP, you can convert an associative array to an array with numeric keys using the array_values() function. This function returns all the values from an array with numeric indexing, starting from 0. Syntax array_values(array $array) Parameters $array − The input array from which to extract values. Return Value Returns an indexed array containing all the values from the input array with numeric keys starting from 0. Example Here's how to transform an associative array into a numerically indexed array ? The output of the ...

Read More

PHP array_push() to create an associative array?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 1K+ Views

In PHP, array_push() is designed for indexed arrays and automatically assigns numeric keys. To create associative arrays with custom key-value pairs, use square bracket notation [] or direct assignment instead. Why array_push() Doesn't Work for Associative Arrays The array_push() function appends values to the end of an array with numeric indexes, which destroys associative keys ? Array ( [name] => John [age] => 25 [0] => Developer ) Creating Associative Arrays with Square Brackets Use square bracket ...

Read More

Can we define constant in class constructor PHP?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 899 Views

No, you cannot define constants in a class constructor in PHP. Constants must be declared at the class level using the const keyword. Once declared, class constants are immutable and can be accessed using the scope resolution operator ::. Syntax Class constants are defined using the following syntax − class ClassName { const CONSTANT_NAME = "value"; } Why Constants Cannot Be Defined in Constructors Constants are resolved at compile time and must have fixed values. The constructor runs at runtime when an object is instantiated, making it incompatible with ...

Read More

Reference assignment operator in PHP to assign a reference?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 399 Views

In PHP, the reference assignment operator =& allows you to create a reference to a variable rather than copying its value. When variables are linked by reference, changes to one variable automatically reflect in the other. Syntax To assign a reference, use the reference assignment operator − $variable1 = &$variable2; This creates a reference where both $variable1 and $variable2 point to the same memory location. Example Here's how reference assignment works in practice − Next Value = 100 Current Value = 100 After changing nextValue: Next ...

Read More

PHP print keys from an object?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 6K+ Views

In PHP, you can extract keys from an object using the array_keys() function. This is useful when you need to access property names dynamically or iterate through object properties. 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 countryName Using array_keys() Method To display only the keys from an object, use array_keys() after ...

Read More

Convert timestamp coming from SQL database to String PHP?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 542 Views

Converting SQL database timestamps to formatted strings in PHP is a common task when working with date and time data. PHP provides the DateTime class with the setTimestamp() method to handle this conversion efficiently. Understanding SQL Timestamps SQL databases often store timestamps as Unix timestamps in milliseconds. To convert them to readable date strings, you need to first convert milliseconds to seconds, then use PHP's DateTime class ? 2020-09-17 05:30:00 Alternative Method Using date() You can also use the built−in date() function for simpler conversions ? ...

Read More
Showing 10151–10160 of 21,090 articles
Advertisements