1. PHP

PHP fgets function

Fungsi fgets digunakan untuk mengambil baris-baris dari suatu file.

Syntax

fgets(resource $handle [, int $length ]
ParameterKeterangan
$handlefile yang akan diambil barisnya
$lengthMenentukan jumlah byte untuk dibaca.
Default adalah 1024 byte. (optional)

Contoh :

Misalkan kita memiliki satu file dengan ekstensi “.txt” yang isinya :

baris1 - senangnya belajar php
baris2 - belajar php itu asik
baris3 - saya suka belajar php

Contoh pertama

<?php
$file = fopen("tes.txt","r");
echo fgets($file);
fclose($file);
?>

Contoh kedua

membaca file baris per baris

<?php
$file = fopen("tes.txt","r");
while(! feof($file))
{
  echo fgets($file);
}
fclose($file);
?>

Contoh ketiga

membaca file baris per baris

<?php
$file = fopen("tes.txt", "r");
if ($file) {
    while (!feof($file)) {
        $buf = fgets($file, 100);
        echo $buf;
    }
    fclose($file);
}
?>

Output dari contoh kedua dan ketiga akan menghasilkan output yang sama

baris1 - senangnya belajar php
baris2 - belajar php itu asik
baris3 - saya suka belajar php
Comments to: PHP fgets function

    Your email address will not be published. Required fields are marked *

    Attach images - Only PNG, JPG, JPEG and GIF are supported.