Scalars and Arrays

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
omniuni
Forum Regular
Posts: 738
Joined: Tue Jul 15, 2008 10:50 pm
Location: Carolina, USA

Scalars and Arrays

Post 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. :banghead:
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!
User avatar
it2051229
Forum Contributor
Posts: 312
Joined: Tue Dec 25, 2007 8:34 pm

Re: Scalars and Arrays

Post by it2051229 »

i dont know but its interesting... can you show line 25?
User avatar
Apollo
Forum Regular
Posts: 794
Joined: Wed Apr 30, 2008 2:34 am

Re: Scalars and Arrays

Post 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 :)
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: Scalars and Arrays

Post by pickle »

~Apollo's right, on both counts.

You probably did something like this:

Code: Select all

$myValue = 3;
$myValue[] = 4;
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
omniuni
Forum Regular
Posts: 738
Joined: Tue Jul 15, 2008 10:50 pm
Location: Carolina, USA

Re: Scalars and Arrays

Post 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: :arrow: 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: :arrow: Posting Code in the Forums to learn how to do it too.
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: Scalars and Arrays

Post 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.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
omniuni
Forum Regular
Posts: 738
Joined: Tue Jul 15, 2008 10:50 pm
Location: Carolina, USA

Re: Scalars and Arrays

Post by omniuni »

ah! I see! So I need to do an extra step if it evaluates to false!

Thanks!
User avatar
Syntac
Forum Contributor
Posts: 327
Joined: Sun Sep 14, 2008 7:59 pm

Re: Scalars and Arrays

Post by Syntac »

pickle wrote:file_get_contents() does not return a serialized string, it returns an array.
Um, last I checked, it returns a string. As in "echo file_get_contents($some_file)".
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: Scalars and Arrays

Post by pickle »

D'oh! You're right.

In any case, the return value from file_get_contents() isn't necessarily an unserializable string.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
Syntac
Forum Contributor
Posts: 327
Joined: Sun Sep 14, 2008 7:59 pm

Re: Scalars and Arrays

Post 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?
Post Reply