PHP Articles

Page 28 of 81

Can we define constant in class constructor PHP?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 889 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 394 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 521 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

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

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

In PHP, you can build a dynamic associative array from a simple array by iterating through the array in reverse order and creating nested associative arrays. This technique is useful when you need to transform a flat array into a hierarchical structure. Syntax The basic approach involves using array_reverse() to process elements from the last to the first, building nested arrays progressively ? function buildDynamicArray($array, $value) { if (empty($array)) { return $value; } ...

Read More

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

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

To insert all combinations of values from PHP arrays into a database table, you can use nested foreach loops to generate the Cartesian product. This technique creates every possible combination of values across multiple arrays. Let's say we have the following arrays − $name1 = ["John", "Mike"]; $name2 = ["David", "Sam"]; $name3 = ["Bob", "Adam"]; Example Here's how to generate SQL INSERT statements for all combinations ?

Read More

Display a two-dimensional array with two different nested loops in matrix form PHP?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 426 Views

In PHP, you can display a two-dimensional array in matrix form using nested loops − one loop for rows and another for columns. Use the tab character \t to create proper spacing between elements. Syntax for ($row = 0; $row < rows; $row++) { for ($col = 0; $col < columns; $col++) { echo $twoDimensionalArray[$row][$col], "\t"; } echo ""; // New line after each row } Example Here's how to create and display a ...

Read More

How to add http:// if it doesn't exist in the URL PHP?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 555 Views

In PHP, you can automatically add "http://" to URLs that don't already have a protocol using preg_match() to check if the URL starts with http or https. This is useful when processing user input or validating URLs. Syntax if (!preg_match("~^(?:f|ht)tps?://~i", $url)) { $url = "http://" . $url; } Example The following function checks if a URL starts with a protocol and adds "http://" if missing ? http://example.com https://example.com ftp://files.example.com http://www.tutorialspoint.com How It Works The regular expression ~^(?:f|ht)tps?://~i breaks down as follows: ...

Read More

Please explain what happens when PHP switch case executes case 0?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 223 Views

In PHP, when a switch statement compares a string value against case 0, PHP's loose type comparison causes unexpected behavior. The string is automatically converted to an integer for comparison, and any non-numeric string converts to 0. Type Conversion in Switch Statements PHP uses loose comparison (==) in switch statements, not strict comparison (===). When comparing a string like "match" with integer 0, PHP converts the string to an integer first. "match" converts 0 ...

Read More

Shorten string with a "..." body in PHP?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 247 Views

In PHP, you can shorten long strings by adding "..." in the middle while preserving the beginning and end portions. This technique is useful for displaying truncated text while keeping important parts visible. Basic String Shortening Here's how to shorten a string when it exceeds a certain length ? This is my first PHP ... gram Flexible Function Approach Create a reusable function to shorten any string ? This is a ve ... ened Short text Parameters Explanation ...

Read More
Showing 271–280 of 802 articles
« Prev 1 26 27 28 29 30 81 Next »
Advertisements