Found 1060 Articles for PHP

fgets() and fread() - What is the difference in PHP?

AmitDiwan
Updated on 07-Apr-2020 13:02:16

2K+ Views

The ‘fgets’ function reads a line and stops when it encounters a newline −The above code opens a text file named ‘test’ in the read mode and reads the contents of the file until a newline character is encountered beginning from the starting byte. The file is then closed.The ‘fread’ function reads raw data and stops after a specific number of bytes or default bytes. This doesn’t depend on whether a newline was encountered or not −The above code opens a text file named ‘test’ in the read mode and reads 10 bytes after the starting byte. The file is ... Read More

How to read a single file inside a zip archive with PHP

AmitDiwan
Updated on 07-Apr-2020 13:00:34

983 Views

To read a single fine inside z zip archive, the code is as follows −$handle = fopen('zip://test.zip#test.txt', 'r'); $result = ''; while (!feof($handle)) {    $result .= fread($handle, 8192); } fclose($handle); echo $result;The output will be the contents of the zip file.

PHP_CodeSniffer, PHPMD or PHP Depend

AmitDiwan
Updated on 07-Apr-2020 12:59:01

182 Views

pdependThe function pdepend is used to generate a large set of software metrics from a given code base. The generated values can be used to measure the quality of a software project. They help in identifying the parts of an application where refactoring is required.phpmdThe phpmd scans the PHP source code and searches for potential problems that could be possible bugs, not-so-optimal code, or overcomplicated expressions.phpcsThe phpcs function tokenises the PHP, JavaScript and CSS files and figures out issues/violations in a set of pre-defined coding standards. It ensures that the code remains consistent and neat. It also helps in preventing ... Read More

Passing static methods as arguments in PHP 

AmitDiwan
Updated on 07-Apr-2020 14:20:40

780 Views

The same syntax used by is_callable and call_user_func can be used to pass static methods as arguments in PHP.To pass the static method, the below example can be used −Example Live Demo OutputThis will produce the following output −bool(true) my_func bool(true)

PHP $string{0} vs. $string[0];

AmitDiwan
Updated on 07-Apr-2020 12:56:04

214 Views

The syntax ‘$string{0} ‘ has been deprecated beginning from PHP version 6. Hence, it is strongly suggested to use $string[0].In short, accessing characters using the flower braces {} has been deprecated. Hence the square brackets should be used [] −Example Live Demo$string = 'medium'; echo $string{0}; echo $string[0];OutputThis will produce the following output −mm

Pass arguments from array in PHP to constructor

AmitDiwan
Updated on 07-Apr-2020 14:19:51

842 Views

The Reflection API can be used to pass arguments from array to constructor.ReflectionClass::newInstanceArgsThe above line creates a new class instance from given arguments −public ReflectionClass::newInstanceArgs ([ array $args ] ) : objectIt creates a new instance of the class when the arguments are passed to the constructor. Here, args refers to the arguments that need to be passed to the class constructor.Example Live DemoOutputThis will produce the following output −object(ReflectionFunction)#2 (1) { ["name"]=> string(6) "substr" }

Encrypting Passwords in PHP

AmitDiwan
Updated on 07-Apr-2020 12:10:49

321 Views

Due to the fact that Blowfish has vulnerabilities before PHP version 5.3.7, it would be suggested to use SHA-256 or SHA-512 instead. Both of them have a similar salt format similar to that of Blowfish (use a prefix of $5$ for SHA-256 and $6$ for SHA-512). In addition to this, it also contains an optional rounds parameter to force multiple hashing.The salt on its own is a little shorter at only 16 characters but unlike Blowfish, it allows more than just alphanumeric characters.Example Live Demoecho 'SHA-256 (no rounds): ' . crypt('password-to-encrypt', '$5$YourSaltyStringz$'); echo 'SHA-512 (with rounds): ' . crypt('password-to-encrypt', '$6$rounds=1000$YourSaltyStringz$');OutputThis will ... Read More

Checking memory_limit in PHP

AmitDiwan
Updated on 07-Apr-2020 11:58:33

1K+ Views

The ‘memory_limit’ is the maximum amount of server memory that a single PHP script is allowed to use. The value needs to be converted before comparing the threshold of memory.For example − 64M is converted to 64 * 1024 * 1024. After this, the comparison is done and the result is printed out.

What is the difference between 'isset()' and '!empty()' in PHP?

AmitDiwan
Updated on 07-Apr-2020 11:56:02

921 Views

Isset functionISSET checks the variable to see if it has been set. In other words, it checks to see if the variable is any value except NULL or not assigned a value. ISSET returns TRUE if the variable exists and has a value other than NULL. That means variables assigned a "", 0, "0", or FALSE are set, and therefore are TRUE for ISSET.Example Live DemoOutputThis will produce the following output −0 is set with isset function array is not set.!empty functionEMPTY checks to see if a variable is empty. Empty is interpreted as: "" (an empty string), 0 (integer), 0.0 ... Read More

Simplest way to detect client locale in PHP

AmitDiwan
Updated on 07-Apr-2020 11:52:30

1K+ Views

PHP provides a function beginning from 5.3.0 to parse the ‘$_SERVER['HTTP_ACCEPT_LANGUAGE']’ variable into a locale −Example$locale = Locale::acceptFromHttp($_SERVER['HTTP_ACCEPT_LANGUAGE']); echo $locale;The ‘$_SERVER['HTTP_ACCEPT_LANGUAGE']’ function helps detect the locale by taking the current locale’s language as the parameter.OutputThis will produce the following output −en_USMost browsers submit an Accept-Language HTTP header that specifies en-us if they are from the US. Some older browsers use en only.English-UK based users usually set their system or user locale to English-UK, which is the default browser configuration. This would result in en-gb as the Accept Language header. Other countries have en locales, such as en-za (South Africa), and ... Read More

Advertisements