Solved

PHP Syntax Question

Posted on 2012-03-17
5
194 Views
Last Modified: 2012-03-18
Hi,

How could I code this?

If the accounts are not equal to

<?php IF $_SESSION['_amember_user']['data']['account_name'] <> test, web, demo
echo "yes"
else echo ""
?>

Open in new window

0
Comment
Question by:Computer Guy
  • 2
  • 2
5 Comments
 
LVL 4

Expert Comment

by:petr_hlucin
ID: 37733005
Try this.
<?php
switch ($_SESSION['_amember_user']['data']['account_name']) {
case "test":
case "web":
case "demo":
    echo "yes";
    break;
default:
    echo "";
}
?>

Open in new window


Other possiblity would be to create an array containing test, web and demo and test whether the variable is part of this array.
0
 
LVL 4

Expert Comment

by:petr_hlucin
ID: 37733071
Sorry, now I've noticed that I've put negation of your condition, the correct code is here:
<?php
switch ($_SESSION['_amember_user']['data']['account_name']) {
case "test":
case "web":
case "demo":
    echo "";
    break;
default:
    echo "yes";
}
?>

Open in new window

0
 
LVL 43

Expert Comment

by:ravenpl
ID: 37734676
probably little easier to write

if (in_array($_SESSION['_amember_user']['data']['account_name'], array('test', 'web', 'demo'))
{...

Refer http://www.php.net/manual/en/function.in-array.php
0
 
LVL 108

Accepted Solution

by:
Ray Paseur earned 500 total points
ID: 37734686
Grab a copy of this little book and give yourself a few weeks to work through the examples.  It will help you get a good foundation in how PHP and MySQL work together - basic syntax, etc.
http://www.sitepoint.com/books/phpmysql4/

In the instant case, this variable is a single data element.  It is a N-level nested array inside the session array.  The bracket notation indicates array elements.   The concatenation of the bracketed elements indicates "sub-arrays."
$_SESSION['_amember_user']['data']['account_name']

So what you're looking at in that variables is the $_SESSION array.  Inside the session array, you have another array, named _amember_user.  Inside the _amember_user array you have another array, named data.  Inside the data array you have a data element named account_name.  This is an unusual data structure to say the least!

More likely you would have something like this...
$_SESSION['_amember_user'] = 'test';
$_SESSION['data'] = 'web';
$_SESSION['account_name'] = 'demo';

If that is the case, this sort of if() construct might make sense (but consider the logic carefully -- do you want the conjunctive or disjunctive association?)...
if ( 
   ( $_SESSION['_amember_user'] != 'test' )
&& ( $_SESSION['data']          != 'web' )
&& ( $_SESSION['account_name']  != 'demo' ) 
   )
   {
      echo "yes";
   }

Open in new window

In my experience, these kinds of compound if() structures are confusing and often the result of awkward data design.  My guess is that you could simplify the data and it would make the programming easier to write, without any compromise in functionality.
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 37735254
Thanks for the points - it's a really good question, ~Ray
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now