AmitDiwan has Published 10740 Articles

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

AmitDiwan

AmitDiwan

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

681 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, ... Read More

How to download large files through PHP script?

AmitDiwan

AmitDiwan

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

3K+ 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 ... Read More

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

AmitDiwan

AmitDiwan

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

334 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. ... Read More

How do I iterate through DOM elements in PHP?

AmitDiwan

AmitDiwan

Updated on 09-Apr-2020 11:34:16

1K+ Views

Following is the XML data (input) −                       Iterating through the elements in the DOM object.Example$elements = $dom->getElementsByTagName('foo'); $data = array(); foreach($elements as $node){    foreach($node->childNodes as $child) {     ... Read More

What does the '@' prefix do in PHP?

AmitDiwan

AmitDiwan

Updated on 09-Apr-2020 11:31:53

785 Views

The ‘@’ symbol suppresses errors from getting displayed on the screen.PHP supports an error control operator, i.e the sign (@). When it is prepended to an expression in PHP, error messages that could get generated when it uses that expression will be ignored.If the track_errors attribute is enabled, the error ... Read More

Is it possible to get list of defined namespaces in PHP

AmitDiwan

AmitDiwan

Updated on 09-Apr-2020 11:31:04

686 Views

Given file 1 has namespace ns_1 and file 2 has namespace ns_2, if file 1 and file 2 are included in file 3, there is no way to know that namespaces ns_1 and ns_2 have been loaded.The only way is to use the ‘class_exists’ function and the list of classes ... Read More

How to open an Excel file with PHPExcel for both reading and writing?

AmitDiwan

AmitDiwan

Updated on 09-Apr-2020 11:28:33

3K+ Views

There is no concept of opening a file for read and write in PHPExcel since it is not aware of the source of the PHPExcel object. Irrespective of the source from where the file was loaded or the type of file, the file can be read based on its named ... Read More

PHP readfile vs. file_get_contents

AmitDiwan

AmitDiwan

Updated on 09-Apr-2020 11:26:26

1K+ Views

The ‘readfile’ function is an inbuilt function in PHP that reads the file directly into the output buffer. The name of the file is passed as a parameter to the function. It returns the number of bytes that were read in case of successfully reading the data. It returns FALSE ... Read More

Storing objects in PHP session

AmitDiwan

AmitDiwan

Updated on 09-Apr-2020 11:25:00

5K+ Views

The serialize() function in PHP can be used before storing the object, and the unserialize() function can be called when the object needs to be retrieved from the session.The function converts a storable representation of a specific value into a sequence of bits. This is done so that the data ... Read More

Generate all combinations of a specific size from a single set in PHP

AmitDiwan

AmitDiwan

Updated on 09-Apr-2020 11:22:49

2K+ Views

To generate all combinations of a specific size from a single set, the code is as follows −Example Live Demofunction sampling($chars, $size, $combinations = array()) {    # in case of first iteration, the first set of combinations is the same as the set of characters    if (empty($combinations)) {   ... Read More

Advertisements