php - Array - Undefined offset even if i already defined it -
i have 2 pages, page 1 contains array this:
$error=array(); $error[]='first value'; $error[]='second value'; $error[]='third value';
page 2 utilize array values:
include 'page1.php'; echo '<p>'.$error[0].'</p>'; echo '<p>'.$error[1].'</p>'; echo '<p>'.$error[2].'</p>';
it should work, instead keeps showing error:
notice: undefined offset: 0 notice: undefined offset: 1 notice: undefined offset: 2
any idea?
from updated question, shows
$error=array(); if( !empty($_post['email']) && isset($_post['email']) ){ if (!filter_var($email, filter_validate_email)) { $error[] = 'emailul nu este valid!'; }else{ $email=$_post['email']; } }else{ $error[]='utilizatorul este obligatoriu!'; }
in included file, should check conditional logic doing var_dumps
var_dump($_post)
, 'email' index.
perhaps status 'email' exists not beingness satisfied!
edited, showing original below...
often you'll find it's less error-prone utilize foreach loop.
foreach($error $err) { echo '<p>'.$err.'</p>'; }
also, may seek using instead of include file.php
, using require('file.php')
or require_once('file.php')
php arrays
No comments:
Post a Comment