Trusted SSL from just $3.44

CategoryEssentials

New Features in PHP 8.0

N

The following are some of the new key features of PHP 8:  JIT (Just in time)   JIT is the most prominent feature of PHP 8. PHP JIT is an independent part of OPcache. It can enable and disable at runtime and compile time. Consider that JIT stands for Just In Time compiler. It is a way of executing computer code during the execution of the program rather than before execution...

Converting float decimal to fraction in php

C

In this solution, we will learn how to convert float decimal to fraction number in PHP. solution 1. PHP function that finds a rational approximation to a given (positive) floating point number with a relative error less than $tolerance. <?php function float2rat($n, $tolerance = 1.e-6) { $h1=1; $h2=0; $k1=0; $k2=1; $b = 1/$n; do { $b = 1/$b; $a = floor($b); $aux = $h1; $h1 = $a*$h1+$h2; $h2 =...

calculate math expression from a string using eval

c

In this solution, we will learn how to calculate math expressions from a string. Don’t use eval because eval expects complete lines of code, not just fragments. solution 1. $ma = "2+10"; if(preg_match('/(\d+)(?:\s*)([\+\-\*\/])(?:\s*)(\d+)/', $ma, $matches) !== FALSE){ $operator = $matches[2]; switch($operator){ case '+': $p = $matches[1] + $matches[3]; break; case...

How can I sort arrays and data in PHP?

H

In this solution, we will learn: solution 1. $array = array(3, 5, 2, 8); How do I sort an array in PHP?How do I sort a complex array in PHP?How do I sort an array of objects in PHP? sorting functions sortrsortasortarsortnatsortnatcasesortksortkrsort Multi-dimensional arrays, including arrays of objects $array = array( array('foo' => 'bar', 'baz' => 42)...

Convert number to month name in PHP

C

This solution will teach us how to convert numbers to month names in PHP. solution 1. Nowadays, you should really be using DateTime objects for any date/time math. $monthNum = 3; $dateObj = DateTime::createFromFormat('!m', $monthNum); $monthName = $dateObj->format('F'); // March solution 2. $monthNum = 3; $monthName = date('F', mktime(0, 0, 0, $monthNum, 10)); // March...

Extract PHP numbers from strings

E

This solution will extract numbers from strings by ignoring 0s and separate into three string numbers. solution 1. $string = '02300103024000100'; $output = array(); // First we replace any '00...' string with just '0' for($a=strlen($string); $a>0; $a--){ $zero_replace = ''; while(strlen($zero_replace) < $a) $zero_replace .= '0'; $string =...

How can I add commas to numbers in PHP?

H

This solution will teach us how to add commas to numbers. solution 1. Assuming the english format solution <?php $number = 1234.56; // english notation (default) $english_format_number = number_format($number); // 1,235 // French notation $nombre_format_francais = number_format($number, 2, ',', ' '); // 1 234,56 $number = 1234.5678; // english notation with a decimal point and...

convert Numbers to Roman Numbers with PHP

c

This solution will teach us how to transform ordinary numbers into Roman numerals with PHP. solution 1. Optimized and prettified function: /** * @param int $number * @return string */ function numberToRomanRepresentation($number) { $map = array('M' => 1000, 'CM' => 900, 'D' => 500, 'CD' => 400, 'C' => 100, 'XC' => 90, 'L'...

How to generate a random, unique, alphanumeric string in PHP?

H

In this solution, you will learn how to generate a random, unique, alphanumeric string in PHP. solution 1. PHP 7 standard library provides the random_bytes() function that generates cryptographically secure pseudo-random bytes. $bytes = random_bytes(20); var_dump(bin2hex($bytes)); The above example will output something similar to: string(40) "5fe69c95ed70a9869d9f9af7d8400a6673bb9ce9"...

Show a number to two decimal places in PHP

S

What’s the correct way to round a PHP string to two decimal places? solution 1. You can use number_format() return number_format((float)$number, 2, '.', ''); Example $foo = "105"; echo number_format((float)$foo, 2, '.', ''); // Outputs -> 105.00 This function returns a string. solution 2. echo round(520.34345, 2); // 520.34 echo round(520.3, 2);...

Tutor Network

Learn PHP from A to Z