Found 1046 Articles for PHP

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

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

160 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

620 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

290 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

967 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

773 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

741 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

Reading/Writing a MS Word file in PHP

AmitDiwan
Updated on 07-Apr-2020 11:50:09

424 Views

Microsoft strongly advises not to use the automation of Office documents via COM objects. It quotes the following −“Microsoft does not currently recommend or support the Automation of Microsoft Office applications from any unattended, non-interactive client application or component (including ASP, ASP.NET, DCOM, and NT Services), because Office may exhibit unstable behavior and/or deadlock when Office is run in this environment.”A .docx file can be created without COM objects since it has XML foundations (PHPDOCX could be used for this). An added advantage of this method is that a local copy of Word that was installed wouldn’t have to be created ... Read More

PHP: recreate and display an image from binary data

AmitDiwan
Updated on 07-Apr-2020 11:48:20

2K+ Views

This can be done using data URI in the image src attribute.Formatdata:[][;charset=""][;base64],

URL Decoding in PHP

AmitDiwan
Updated on 07-Apr-2020 11:45:18

456 Views

URL decoding can be done using the built-in 'urldecode' function. This returns the encoded data.Syntax of urldecode functionstring urldecode($input)It takes a single parameter ($input) which is the URL that is to be decoded. Returns the decoded string provided the decoding was successful −Example Live Demo In the above lines of the code, the ‘urldecode’ function takes in the raw (encoded string) and returns the decoded value of the string.OutputThis will produce the following output −https://medium.com/

Convert ASCII TO UTF-8 Encoding in PHP?

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

7K+ Views

If we know that the current encoding is ASCII, the 'iconv' function can be used to convert ASCII to UTF-8. The original string can be passed as a parameter to the iconv function to encode it to UTF-8.Example Live DemoA string with special characters is assigned to ‘str’ variable. This is passed to the ‘iconv’ function, with the encoding that it currently is in, and the encoding to which it needs to be converted to.OutputThis will produce the following output −Original :ábrêcWtë Plain :�br�cWt�Another method is to detect the encoding and then converting it to an appropriate encoding −Example Live Demo$string = ... Read More

Advertisements