Found 1046 Articles for PHP

PHP abs() Function

Malhar Lathkar
Updated on 11-Jun-2020 08:30:57

2K+ Views

Definition and UsageThe abs() function is an in-built function in PHP iterpreter. This function accepts any number as argument and returns a positive value, disregarding its sign. Absolute value of any number is always positive.This function always returns positive number.Syntaxabs( mixed $num)ParametersSr.NoParameter & Description1numThis parameter stores a value whose absolute value is to be obtained.Return ValuesPHP abs() function returns absolute value of num. If data type of num is float, return type is also float.For integer parameter, return type is integer.PHP VersionThis function is available in PHP versions 4.x, PHP 5.x as well as PHP 7.x.Example Live DemoFollowing example shows that ... Read More

Difference between bindParam and bindValue in PHP

Nitin Sharma
Updated on 09-Jun-2020 08:30:50

4K+ Views

Both bindParam and bindValue are the inbuilt functions of PHP which are used for accessing database records by mapping variable to the value in PHP data objects statement also known as PDOStatement which is nothing else but is an abstraction layer for database queries.Following are the important differences between ASP and ASP.NET.Sr. No.KeybindParam functionbindValue function1DefinitionbindParam is a PHP inbuilt function used to bind a parameter to the specified variable name in a sql statement for access the database record.bindValue, on the other hand, is again a PHP inbuilt function used to bind the value of parameter to the specified variable ... Read More

How to prevent multiple inserts when submitting a form in PHP?

AmitDiwan
Updated on 09-Apr-2020 13:40:58

2K+ Views

PHP session can be used to prevent multiple inserts when submitting a form. PHP session sets a session variable (say $_SESSION['posttimer']) that sets the current timestamp on POST. Before processing the form in PHP, the $_SESSION['posttimer'] variable is checked for its existence and checked for a specific timestamp difference (say 2 or 3 seconds). This way, those insertions that are actually duplicates can be identified and removed.Simple form −// form.html         The reference to ‘my_session_file.php’ in the above will have the below lines of code −Exampleif (isset($_POST) && !empty($_POST)) {    if (isset($_SESSION['posttimer'])) {     ... Read More

PHP Casting Variable as Object type in foreach Loop

AmitDiwan
Updated on 09-Apr-2020 11:44:45

693 Views

This depends on the IDE that is being used. For example, Netbeans and IntelliJ can enable the usage of @var in a comment −/* @var $variable ClassName */ $variable->This way, the IDE would know that the ‘$variable’ is a class of the ClassName after the hint ‘->’ is encountered.In addition, an @return annotation can be created with a method that specifies that the return type will be an array of ClassName objects. This data can be accessed using a foreach loop that fetches the values of the objects −function get_object_type() {    return $this->values; } foreach( $data_object-> values as $object_attribute ... Read More

Sort php multidimensional array by sub-value in PHP

AmitDiwan
Updated on 09-Apr-2020 11:43:24

270 Views

The ‘usort’ function can be used to sort multidimensional arrays in PHP. It sorts an array based on a user-defined criteria −Example Live DemoOutputThis will produce the following output −2 4 63 81An array with 4 elements is declared and this array is passed to the usort function, as well as calling the user-defined ‘my_sort’ function on the elements to make sure that the sorting takes place is ascending order.

In PHP, how can I add an object element to an array?

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

3K+ Views

The code is as follows −Example$object = new stdClass(); $object->name = "My name"; $myArray[] = $object;OutputThis will produce the following output −Suppose myArray already contains ‘a’ and ‘c’, the value of “My name” will be added to it. It becomes Array {    a:0, c:1, “My name”:2 }The object is created and then it is pushed to the end of the array (that was previously present).Alternative$myArray[] = (object) ['name' => 'My name'];

Detect language from string in PHP

AmitDiwan
Updated on 09-Apr-2020 11:40:36

756 Views

The language can’t be detected from the character type. There are other ways, but they don’t guarantee complete accuracy. The ‘TextLanguageDetect Pear Package’ can be used with decent accuracy. Below is a sample code for the same −Examplerequire_once 'Text/LanguageDetect.php'; $l = new Text_LanguageDetect(); $result = $l->detect($text, 4); if (PEAR::isError($result)) {    echo $result->getMessage(); } else {    print_r($result); }OutputThis will produce the following output −Array (    [german] => 0.407037037037    [dutch] => 0.288065843621    [english] => 0.283333333333    [danish] => 0.234526748971 )It is easy to use and has 52 language databases. But the downside is that the Eastern Asian languages can’t be detected using this package.

How to read only 5 last line of the text file in PHP?

AmitDiwan
Updated on 09-Apr-2020 11:39:18

386 Views

To read only 5 last lines of the text file, the code is as follows −Example$file = file("filename.txt"); for ($i = max(0, count($file)-6); $i < count($file); $i++) {    echo $file[$i] . ""; }OutputThis will produce the following output −Given that the file has more than 5 lines of text, the last 5 lines of the text file will be displayed.The file is opened and the number of lines in the file are counted, and beginning from the last line, 5 lines are read.

How to download large files through PHP script?

AmitDiwan
Updated on 09-Apr-2020 11:37:51

2K+ Views

To download large files through PHP script, the code is as follows −ExampleOutputThis will produce the following output −The large file will be downloaded.The function ‘readfile_chunked’ (user defined) takes in two parameters- the name of the file and the default value of ‘true’ for the number of bytes returned meaning that large files have been successfully downloaded. The variable ‘chunksize’ has been declared with the number of bytes per chunk that needs to be read. The ‘buffer’ variable is assigned to null and the ‘cnt’ is set to 0. The file is opened in binary read mode and assigned the ... Read More

Make PHP pathinfo() return the correct filename if the filename is UTF-8

AmitDiwan
Updated on 09-Apr-2020 11:35:56

204 Views

Most of the core PHP functions don’t deal with character sets apart from Latin-1. But before the ‘pathinfo’, placing the ‘setlocale’ can be used to return the correct filename even if it is UTF-8 encoded.By default, it runs with ‘C’ locale, and CLI scripts run with a default utf-8 locale. The locale on the server should be changed from ‘C’ to ‘C.UTF-8’ or to ‘en_US.UTF-8’ before calling the other functions.setlocale(LC_ALL,'en_US.UTF-8'); pathinfo($OriginalName, PATHINFO_FILENAME); pathinfo($OriginalName, PATHINFO_BASENAME);

Advertisements