Jacob Buller
Jacob Buller

Reputation: 137

Simpler version of my PHP

I'm currently making a very rudimentary CMS, not using an OOP just simple PHP and MySQL queries. In the CMS part of the site I have form for updating the site's content and I have certain fields displayed depending on the item that is selected in the navigation - via passing the items ID in the URL and using the _GET variable to query it. I am trying to have an 'Author' input display only on two items and I am using the code below to only display it when it's value isn't equal to these three numbers...

 <?php if(($current_page["subject_id"] != 16) && ($current_page["id"] != 7) && ($current_page["id"] != 10)) {?>

Is there an easier way to do this? I know the whole point of being a good coder is to simplify the code... I think I'm failing with this one.

Upvotes: 1

Views: 56

Answers (2)

David
David

Reputation: 4883

That code is as basic as it gets. If you want things a tad more simple, then you could assign $current_page["id"] to another variable to get rid of the array access overhead, but really, that isn't worth mentioning (Unless your array is huge, and still, even then it's not doing a lot). Using a function like in_array that has been suggested is also not much better in speed or simplicity. I'd say just keep it like you have.

On another note, if you're accessing those arrays more than just in that if statement, then it would be a good idea to assign them to variables. Otherwise, what you have is good.

Upvotes: 1

عثمان غني
عثمان غني

Reputation: 2708

You can just use in_array here.

For e.g.:

<?php
$id=array('7','10');
if(($current_page["subject_id"] != 16) && !in_Array($current_page["id"],$id)) {?>

If you have many number of ids you just need to pass IDs to $id.

Upvotes: 0

Related Questions