Page 1 of 1
Scalars and Arrays
Posted: Thu Jan 15, 2009 2:24 am
by omniuni
I am trying to prepare a small script to use for a demonstration, and I am getting an odd error. It only happens in one condition, and only once, after which the script works fine, even though I don't see what's different the second time through.
Warning: Cannot use a scalar value as an array in /home/daniel/public_html/codescraps/notes_class.php on line 25
For starters, any idea what exactly that means?
Thanks!
Re: Scalars and Arrays
Posted: Thu Jan 15, 2009 2:40 am
by it2051229
i dont know but its interesting... can you show line 25?
Re: Scalars and Arrays
Posted: Thu Jan 15, 2009 3:01 am
by Apollo
Baiscally speaking, a 'scalar value' is just a single value, i.e. an integer or string, as opposed to an array which is a list of values.
Apparently you mixed them up. It would help a lot if you posted your actual code though

Re: Scalars and Arrays
Posted: Thu Jan 15, 2009 10:06 am
by pickle
~Apollo's right, on both counts.
You probably did something like this:
Re: Scalars and Arrays
Posted: Thu Jan 15, 2009 1:05 pm
by omniuni
~pickle | Please use [ code=html ], [ code=php ], etc tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:
Posting Code in the Forums to learn how to do it too.
@Pickle: I considered that, but I don't see how exactly that's happening. This is for a short presentation, there are two PHP files, one that contains a class.
notes_class.php
Code: Select all
<?php
class noteList{
private $noteFile = 'notes.data';
public $asArray;
public function __construct(){
if(is_file($this->noteFile)){
$this->asArray = unserialize(file_get_contents($this->noteFile));
}else{
$this-asArray;
}
}
public function view($id){
echo $this->asArray[$id].'
<br/><br/>
<a href="?remove='.$id.'">Remove Note</a>
';
}
public function write($note){
//these two produce errors when trying to write for the first time, but then are OK.... wt*!? <-- Pardon my bad language in my comments
$this->asArray[] = $note;
$this->asArray = array_values($this->asArray);
file_put_contents($this->noteFile, serialize($this->asArray));
}
public function remove($id){
unset($this->asArray[$id]);
$this->asArray = array_values($this->asArray);
file_put_contents($this->noteFile, serialize($this-asArray));
}
}
?>
notes_with_class.php
Code: Select all
<html>
<head>
<title>Notes With Class</title>
</head>
<body>
<h1>My Notes With Class</h1>
<hr/>
<?php
require_once('notes_class.php');
$myNotes = new noteList;
if(isset($_GET['remove'])){
//echo 'Remove a Note';
$myNotes->remove($_GET['remove']);
}
if(isset($_POST['new-note'])){
$myNotes->write($_POST['new-note']);
}
if(isset($_GET['view'])){
//echo 'View a Note';
$myNotes->view($_GET['view']);
}else{
if($myNotesArray = $myNotes->asArray){
foreach($myNotesArray as $id => $preview){
echo $id.'| <a href="?view='.$id.'">'.substr($preview, 0, 20).'...</a><br/>';
}
}
}
?>
<hr/>
<form action="?" method="post">
<textarea name="new-note" rows="3" cols="20"></textarea>
<br/>
<input type="submit" value="Write New Note"/>
</form>
You can play with the scripts online at
http://remote.d-site.net/codescraps/not ... _class.php
~pickle | Please use [ code=html ], [ code=php ], etc tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:
Posting Code in the Forums to learn how to do it too.
Re: Scalars and Arrays
Posted: Thu Jan 15, 2009 3:24 pm
by pickle
Your problem stems from this line:
Code: Select all
$this->asArray = unserialize(file_get_contents($this->noteFile));
file_get_contents() does not return a serialized string, it returns an array. Consequently,
unserialize() returns boolean FALSE - a scalar value. Later, in write() you try to treat $this->asArray as an array.
Edit: As ~ Syntac said below, file_get_contents() returns a string, not an array.
Re: Scalars and Arrays
Posted: Thu Jan 15, 2009 5:41 pm
by omniuni
ah! I see! So I need to do an extra step if it evaluates to false!
Thanks!
Re: Scalars and Arrays
Posted: Thu Jan 15, 2009 5:45 pm
by Syntac
Um, last I checked, it returns a string. As in "echo file_get_contents($some_file)".
Re: Scalars and Arrays
Posted: Thu Jan 15, 2009 5:51 pm
by pickle
D'oh! You're right.
In any case, the return value from file_get_contents() isn't necessarily an unserializable string.
Re: Scalars and Arrays
Posted: Thu Jan 15, 2009 5:56 pm
by Syntac
While we're on the topic of serialization, didn't someone write a library for unserializing data that causes PHP's functions to choke?