hi, first let it be known to anyone answering my question that i am n00b to the php coding but i have been playing with php for more than a year or so, now i have a problem:
i have a structure like this(as i learnt in C++, i guess there is something similar in php too):
Struct student
{
int roll;
char name;
int class;
}
i want to write or append an object of this structure in a file named 'datafile.dat' on my server and read it later to display some specific object in other php file. i may have to edit the object data too some time. so is there any code for this?.. yeah there are a lot, but i cant get anything. so only codes and explanations needed here. if you can please tell me how to read write and edit a structure in to a file through php on server. (many would say i would have used database for it, but man, mind it, i have no extra bucks). thanks in advance.
problem on php file read-write-edit
Moderator: General Moderators
Re: problem on php file read-write-edit
Look up the function serialize and unserialize they be what you are looking for. (see http://us.php.net/manual/en/language.oo ... zation.php)
-Greg
-Greg
Re: problem on php file read-write-edit
In PHP, the thing most analogous to C's struct is this.dentrite wrote:i have a structure like this(as i learnt in C++, i guess there is something similar in php too):
Struct student
{
int roll;
char name;
int class;
}
Code: Select all
class Student
{
var $roll;
var $name;
var $class;
}
$myStudent = new Student();
$myStudent->roll = 21;
$myStudent->name = 'My Student';
$myStudent->class = 42;
var_dump($myStudent->name);Code: Select all
class Student
{
var $roll;
var $name;
var $class;
function __construct ($roll, $name, $class) {
$this->roll = $roll;
$this->name = $name;
$this->class = $class;
}
}
$myStudent = new Student(21, 'My Student', 42);
var_dump($myStudent->name);Code: Select all
$myStudent = new stdClass();
$myStudent->roll = 21;
$myStudent->name = 'My Student';
$myStudent->class = 42;
var_dump($myStudent->name);Code: Select all
$myStudent = array (
'roll' => 21,
'name' => 'My Student',
'class' => 42,
);
var_dump($myStudent['name']);No extra bucks needed. PHP has a built-in database management system for SQLite (SQLite3, PDO SQLite).(many would say i would have used database for it, but man, mind it, i have no extra bucks)
See also: Filesystem Functions