top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

How to read the contents of a file in PHP.?

0 votes
479 views
How to read the contents of a file in PHP.?
posted Jun 6, 2014 by Amritpal Singh

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

2 Answers

0 votes

To define a constant you have to use define() function and to retrieve the value of a constant, you have to simply specifying its name. Unlike with variables, you do not need to have a constant with a $. You can also use the function constant() to read a constant's value if you wish to obtain the constant's name dynamically.

answer Jun 6, 2014 by Rahul Singh
0 votes

Following program can be used for reading the contents of a file in php.
<?php

$fileName = 'instructions.txt';
//fopen() function is used to open a file in php

$file = fopen(“instructions.txt”,”r”) or exit(“Unable to open the file!”) ;

While(!feof($file)) // feof()checks the end of file in php

{
echo fgets($file); // fgets() read a fileline by line in php
}
fclose($file); // fclose() used to close a file in php
?>

answer Jun 9, 2014 by Karamjeet Singh
Similar Questions
0 votes

The output is in this form

Array (
     [0] => name
     [1] => email
     [2] => contact
     [3] => address 
    ) 
Array (
     [0] => sant
     [1] => sant@gmail.com
     [2] => **********
     [3] => haryana ) 

But I want the output in this form

  Array[1] (
     [0] => name
     [1] => email
     [2] => contact
     [3] => address 
    ) 
Array[2] (
     [0] => sant
     [1] => sant@gmail.com
     [2] => **********
     [3] => haryana
    ) 
+1 vote

Why I can't read static variables of a child class, even so the variable is protected or public?

Here's what I tried:

class A {
 protected static $foo = "bar";
}

class B {
 public function readFoo() {
 return self::$foo;
 }
}

When now calling B::readFoo, I get the following error:

var_dump((new B())->readFoo());

PHP Fatal error: Access to undeclared static property: B::$foo in php shell code on line 1
PHP Stack trace:
PHP 1. {main}() php shell code:0
PHP 2. B->readFoo() php shell code:1

Fatal error: Access to undeclared static property: B::$foo in php shell
code on line 1

Call Stack:
 86.2643 231224 1. {main}() php shell code:0
 86.2666 231352 2. B->readFoo() php shell code:1
...