Jump to content



  • Please log in to reply
2 replies to this topic

#1 KenJuma

  • New Members
  • Pip
  • Newbie
  • 1 posts

Posted 23 January 2013 - 01:22 PM

I'm new to php and my first assignment is to design a project that give a user synonyms to words and also allows the user to add words and their synonyms. The thesaurus doesnt have to be full of all the words, an example of two or three words would just be enough. I do not want to use a database since I want to fullu understand php before starting to interact with databases. Someone told me I could save the words in some file which will be accessed by the codes and when the user enters the new words, they would automatically be updated in the file. I was thinking of using arrays, any help..?

#2 Jessica

  • Staff Alumni
  • This is not my name.
  • 8,982 posts
  • LocationDallas, TX
  • Age:26

Posted 23 January 2013 - 02:57 PM

You cannot "fully understand" PHP without interacting with a database. PHP and MySQL really go hand in hand, especially when you're talking about storing data.

But yes, you can store data in a file. You can find many tutorials on file read/write if you google it. 
My goal in replying to posts is to help you become a better programmer, including learning how to debug your own code and research problems. For that reason, rather than posting the solution, I reply with tips and hints on how to find the solution yourself. See below for useful links when you get stuck.

How to Get Good Help: How to Ask Questions | Don't be a help vampire
Debugging Your Code: Debugging your SQL | What does a php function do? | What does a term mean? | Don't see any errors?
Things You Should Do: Normalize Your Data | use print_r() or var_dump()
Lulz: "Functions should not have side effects." - trq

Please take a look at my new PHP/Web Dev blog: The Web Mason - Thanks!!

#3 Andy123

  • Members
  • PipPipPip
  • Advanced Member
  • 134 posts
  • LocationDenmark

Posted 23 January 2013 - 04:38 PM

In a "real" application, one would of course use a database for this, but I understand that you want to move forward slowly and not learn everything at once. As Jessica said, you would almost always be using a database for storage, so learning how to do this by file probably won't help you out that much in the future anyways. Using a database also makes it easier to make relationships between data such as words. For instance, one could make relationships between words that indicate that they are synonyms instead of replicating the words in PHP (avoiding this would probably get more complicated than it really has to be).

Either way, here is a quick example of how you could use arrays in PHP for this. Surely there are better ways (especially if you use a database), but perhaps it is enough to get you started.

$words = array(
'beautiful' => array('handsome', 'pretty', 'lovely'),
'funny' => array('comical', 'amusing', 'humorous', 'laughable'),
'clever' => array('smart', 'intelligent', 'ingenious')

To save it to a file, you can can serialize the array (Google should help you out here).

Each word has an array of synonyms. Imagine that for each of those words, you would have an array of all of the other synonyms. For instance, an entry for the word "smart" would have an array of "clever", "intelligent", "ingenious", and so on. You can use references, but I think you will end up overcomplicating things - and worse yet, you will probably not be able to leverage so much from the experience since it is probably far away from what you will face when doing "real" applications. So my advice is to keep it as close to how you would do it if you were paid to do it for someone.

Good luck!
Blogging about PHP and other programming related subjects at CodingExplained.com.

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users