Jump to content

Weird error: "Unsupported operand types" when I run this script ...


MoMoMajor

Recommended Posts

<?php
session_start();  
if(isset($_SESSION['views'])) {
    $_SESSION['views'] = $_SESSION['views']+ 1;
} else {
    $_SESSION['views'] = 1;
echo "views = ". $_SESSION['views']; 
}
?>

 

When I run the above script I get this error : Fatal error:

 

Unsupported operand types in C:\xampp\htdocs\Website\sessions.php on line 4

 

I don't know why this is happening especially since I just copied and pasted this code from tizag.com php tutorials.

 

I did modify the code slightly ... it was originally: 

 


<?php
session_start();  
if(isset($_SESSION['views']))
    $_SESSION['views'] = $_SESSION['views']+ 1;
else
    $_SESSION['views'] = 1;

echo "views = ". $_SESSION['views']; 
?>

 

I just added some {} around the if statements.

 

Any help would be appreciated.

Link to comment
Share on other sites

Try this instead and let me know...

 

if (isset($_SESSION['views']))
{
if (!is_numeric($_SESSION['views']))
{
echo "CRAP!";
}
++$_SESSION['views'];
}else{
$_SESSION['views'] = 1;
}

 

When I run the script all I get is a blank page.

 

 

P.S - I've seen this error before by the way, what version of PHP are you running? Echoing phpinfo() will tell you this.

 

I am running 5.2.6

Link to comment
Share on other sites

If it's blank it could mean:

 

a) That the session variable "views" isn't set or that

b) There's an error but you don't have error reporting turned on.

 

Try

 

error_reporting(E_ALL);
if (isset($_SESSION['views']))
{
if (!is_numeric($_SESSION['views']))
{
echo "CRAP!";
}
++$_SESSION['views'];
}else{
$_SESSION['views'] = 1;
}

Link to comment
Share on other sites

Well that means it's probably fixed. You just need to add in...

 

echo "views = ". $_SESSION['views']; 

 

To view the session views variable. Your final script should look like...

 

<?php

session_start();

if (isset($_SESSION['views']))
{
if (!is_numeric($_SESSION['views']))
{
echo "CRAP!";
}
++$_SESSION['views'];
}else{
$_SESSION['views'] = 1;
}

echo "views = ". $_SESSION['views'];

?>

Link to comment
Share on other sites

If it's blank it could mean:

 

a) That the session variable "views" isn't set or that

b) There's an error but you don't have error reporting turned on.

 

Try

 

error_reporting(E_ALL);
if (isset($_SESSION['views']))
{
if (!is_numeric($_SESSION['views']))
{
echo "CRAP!";
}
++$_SESSION['views'];
}else{
$_SESSION['views'] = 1;
}

 

I think he just copied my code directly rather than integrating it into his own script and I hadn't included the echo command which is why he didn't see anything.

Link to comment
Share on other sites

If it's blank it could mean:

 

a) That the session variable "views" isn't set or that

b) There's an error but you don't have error reporting turned on.

 

Try

 

error_reporting(E_ALL);
if (isset($_SESSION['views']))
{
if (!is_numeric($_SESSION['views']))
{
echo "CRAP!";
}
++$_SESSION['views'];
}else{
$_SESSION['views'] = 1;
}

 

Still blank. I get a lot of error messages so that would mean they are turned on right? Or is their more then one setting?

 

 

 

Well that means it's probably fixed. You just need to add in...

 

echo "views = ". $_SESSION['views']; 

 

To view the session views variable. Your final script should look like...

 

<?php

session_start();

if (isset($_SESSION['views']))
{
if (!is_numeric($_SESSION['views']))
{
echo "CRAP!";
}
++$_SESSION['views'];
}else{
$_SESSION['views'] = 1;
}

echo "views = ". $_SESSION['views'];

?>

 

Return is :

 

CRAP!views = Array

Link to comment
Share on other sites

Oh wait ... I did just c/p into the code. I just added session_start()

 


<?php

session_start(); 

error_reporting(E_ALL);
if (isset($_SESSION['views']))
{
if (!is_numeric($_SESSION['views']))
{
echo "CRAP!";
}
++$_SESSION['views'];
}else{
$_SESSION['views'] = 1;
}
?>

 

it returns: CRAP!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.