Using Switch() VS Creating Multiple Scripts? [closed] - php

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 6 years ago.
Improve this question
Actually I have a architecture question. I want to know, should I create multiple separate scripts (.php files) for some tasks which have both relevant together and different jobs? Or should I create just one script and use switch() function (of if-statement) to separate them of each other?
Let me say an example. I have a question-and-answer (Q&A) website. I want to know should I create two separate scripts for writing a question and writing an answer or should I create just one script for storing both answers and questions?
In other word:
One: One script
Storing_answers_questions.php
if ($_[POST] == 'q') {
// codes for storing a question are here
} else {
// codes for storing a answer are here
}
And then the action of both forms (question, answer) will be:
www.example.com/Storing_answers_questions.php
Two: Multiple scripts
Storing_answers.php
// codes for storing a answer are here
// ------------------------------------------------------------
Storing_questions.php
// codes for storing a question are here
And then the action of question-form will be:
Storing_questions.php
And answer-form will be:
Storing_answers.php
Well, which way?

I think it doesn't fall in the performance. It's a question of maintainability.
The question now is why and how?
Once your codes becomes long, it will be very confusing to look at your code. It will be harder to find what you're looking for. If you have seperated and organize files, it will lessen the risk for that to happen.
Real life scenario:
You have files in your computer, There is phptest.php, jstest.js,
jquerytest.js, csstest.css in one folder. Isn't it easier to find if you
seperate them in folder.
PHP->phptest.php
JavaScript->jstest.js, jquerytest.js
CSS->csstest.css
Seperated Files is also best for team project. You can assign this group to write a code for questions , and this group is for answers without stepping on their toes (Even if you are using version controls). They are organize.
And that's it, if you think this will have a very long complicated codes. Go for seperation.

In my experience the one-file style for this sort of thing would be typical, unless you have a deliberate flow in mind.

Related

How to make HTML interact with a database? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
Hello everyone, I have a project and I want to create an MVP: Minimum Viable Product.
And I have html pages ready but I don't know how to make them interact with databases, preferably MySQL.
I need the simplest way to be able to POST and GET data from a database, PHP or Rails.
IF there's just one simple tutorial or a small ebook to read, I'll be so thankful.
I have to the end of the month to create the MVP, about 20 days or so, and I really want to make that happen and I was gonna learn the whole Rails just to do that, but it'll take time you know!
Thanks for your time and I hope that you might help me :)
You can't make HTML directly interacting with database. You should create server-side application, which answer queries generated by HTML forms, JS queries, etc. I am PHP developer, I like this language, so I recommend you using it in your solution.
You can read about connecting PHP to MySQL database here:
http://www.w3schools.com/php/php_mysql_connect.asp
There you have basic information about handling data sent by POST:
http://www.w3schools.com/php/php_forms.asp
If you have any troubles during develop proccess try Google before, then if didn't find answer ask specific and well described question on Stack Overflow.
Best luck!

virtual pages using PHP only? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I am working on a project that I need to let users to create pages on my server. however, I do not want to let users clutter my mysql database by storing the stuff in there so I cannot use mysql database for creating the pages.
I did research this topic and there seem to be a some sort of a plugin for WP that will allow virtual page creation.
is this possible using pure php WITHOUT the use of any database ?
It's possible, but wrong.
You can use php to write a html file to your web directory, sure. But that
solution is in no way cleaner or less cluttered than putting stuff in your
database, for a few reasons:
It's easier to have structured information in the database
It's a good thing conceptually to separate user data from your program
It's easier to control access to your database in a safe way, compared
to writing user data to the file system
"I really do not want to use mysql database" is not a good reason to give this
up. You might have a good reason, but it's not easy to guess what that is, which
makes suggesting alternatives very difficult.

Can too many defined constants become counter productive? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
Is this a pitfall that some have encountered? I have only recently discovered the notion of defining a constant to use globally but find myself attempting to define away each and every repetitive block of code (no matter how long).
Can I get two major reasons when to avoid using constants and one supportive reason/example illustrating great use?
The reason not to use constants - Having global data is typically considered bad practice. Since PHP has the ability to be object oriented, most well written software takes advantage of it. Here is a pretty good explanation without getting too lengthy about encapsulation and abstraction.
The reasons I consider using them, typically for things that are reusable, that I would like to change all at once. an example i can think of is contact information. It's usually in the footer, sometimes the header and typically on a contact page. If I want to reuse a template I can just change the information in one spot and effectively update it on the entire site. Usually the database credentials live in the same file. I'm not saying this is the best practice, but it's convenient to only have to make changes to one file for global changes to a website.
That's just my 2 cents.

Multilingual website, translations stored in a local.php file or in the database? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
I have a simple question! I want to create my website with multilingual content, but i have a doubt about which is the right way to do this.
Option 1: Create a .php file called for example lang.en.php (where there is an array of all the strings I use on the page) and include it in the header.php file, so that to add a new language i only have to create new files like lang.es.php
Option 2: Store the strings in a database table and take them with a query.
Now my question is this: when i will upload the website on a real server, which one is the faster method? which one slows down less?
There is also an option 3 use mo/po files which are specialized for the task and also kinda nicely handle singulars / plurals for you.
However being sanely able to edit those files you would need to provide a GUI. But that should also be the case when you are using the array in a php file approach.
It's pretty hard to tell which way is the way ™ so I think I might close your question as too subjective.
When you are going for the database approach I would personally have a way to export the texts to either a po file or a array in a php file to prevent having to query the database for those mostly static texts.
To answer you question po files might be faster, however with opcache it might be pretty close to eachother. If you really want to know it the best thing you can do is do some tests with either approach because I just simply pulled the speed of po vs static array in a file out of my arse :-)
P.S. when you are benchmarking both methods also please keep in mind that whichever method you are going to choose performance is most likely not going to be the bottleneck so choosing one or another for performance is probably a bullshit reason.

Having a really long PHP file [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 9 years ago.
Improve this question
I'm kinda new to PHP and I am writing a new script, so I have a general wondering.. Is it okay to have a large PHP file? Or I should just split it? By large I mean 3000 Lines <
Everything is working fine, I just want to know if this may affect the response time or something..
There is no problem with large PHP file. But if you want to divide your code in separate files this will help you in fixing the bug and in code reusability.
It will not affect in response time or performance of your code.
Splitting the file will not help, because presumably you'd just be loading the code from multiple files instead of one file.
In fact, it has been shown that performance gets significantly better if you append files together instead of forcing the PHP request to load a lot of individual PHP code files.
If you are at all concerned with performance, you should be using a PHP bytecode cache.
In any case it will have the best response time with the given code since it will need to read that one file only.
The problem with having a single monolithic file is it will make it harder to maintain
try to improve reusabilty and readability of your code. There are no rules about the lenght of your code.

Categories