Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

PHP AI with Php

TheJason

Active Coder
PHP is cool in that you don't need a compiler. O.K.,, in that case, I've been wanting to do some AI (artificial intelligence) stuff with php. What are some basic things that might apply to the situation? Are there any good tutorials out there?
 
I would advise against doing stuff of that level in PHP. Python will have better tools available. PHP is simple scripting for a web server that outputs hypertext. If you want to do stuff like machine learning, python is much more of an industry standard. This stuff is complicated so be prepared to do a lot of research. There's also no general AI yet, just clever algorithms that are good at adapting to data, processing human language, stuff like that.
 
@Cosmic is right @TheJason. Artifical intelligence would be better suited for what Cosmic has said or C++, C#, Java... etc Things like that

Personally find the one you think is best for you in my opinion and go for it. Learn it, pull a library into your project and make something unique. Might take a while to learn but once you learn it then you will have the skill set you need to get really good.
 
AI is incredibly complex & requires a lot of data for it to go well
PHP is not well equipped to handle this type of thing, but it CAN be done

However, it will not be as artificially intelligent as you may hope
You can give PHP a bunch of guidelines & rules, and you can use PHP to access tons of data, analyze it, and make a decision based on what it knows... but it's just not as efficient. PHP is a server language that is commonly accessed via internet browsers. Simply put, there are better languages out there for AI than PHP...

But...
here's how it would work - we'll use tic tac toe as an example:
  1. give PHP the rules... aka: you can't go twice in a row (wait your turn), you can't place an X or an O on a spot already taken, and if three Xs or three Os are in a row/diagonal, that player wins...
  2. use PHP to store every tic tac toe move to a database... record the full games from first/starting move to the end
  3. Tell your little PHP AI to search database for "similar" scenarios to the one currently on the board... as more games are played, there are more similar scenarios to analyze... because tic tac toe is a 3x3 board, the board can be "rotated" to cut down on the # of possibilities. for ex, an X in the left corner as the starting move is the same as an X in the right corner for the first move, if it's rotated... this can limit the # of total possible scenarios/moves your AI has to analyze - so that speeds it up.
  4. Don't tell your AI anything about strategy, just give it the option of analyzing past games & moves for each scenario / whether it won or lost
  5. Tell your AI that it always should pick the move that can win it the game or at least tie - but not lose
With some basic rules in place, we don't have to tell our AI how to 100% tie or win in tic tac toe. However, since tying is always 100% possible, if you pit your AI against your own AI they will eventually play so many games that they will know how to 100% tie the game, and there will never be a win or a loss because both AIs will know exactly what to do to prevent a loss - even if it means a tie. Against a human player, the AI may even learn to take risks... for example if you put your AI against an extremely bad player for 2,000 matches, then the bad player might not always stop the AI from winning...the bad player might even forget to "win the game" by placing an X/O in a spot when they could've won - if this happened enough, the AI would learn to believe that the opposing player will never choose to finish a row/diagonal and instead of blocking the opposing player from winning, the AI might do something else... But against an amazing player the AI would eventually learn that the player doesn't make mistakes and risks aren't worth it.

So yes, you can create an AI with PHP, but it's not as fast as with other languages
Tic tac toe is a very simple example. A harder example would be chess.
The problem with AI is that you can easily create a PHP bot to 100% NEVER lose (always win or tie) a tic tac toe game, but it's harder to code the bot so that it LEARNS how to always win/tie... there's a huge difference. If you tell it how to always win/tie, it's not AI... It's just a programmed bot. So there's a fine line between "telling the bot the rules" and "telling the bot how to win". It just comes down to time.

You can create an AI bot with pretty much any language as long the language allows you to:
- store a large amount of data for past results/events (ex: game, stock market, coin flip, etc) ~ must be able to add data to a database
- analyze the past data - needs to be able to retrieve data from a database
- figure out the best choice based on past data / current scenario - use the rules of the game to apply to past data sets, without specifically being told a strategy

At Wubur, (Wubur.com) our website development company, we use a hybrid of AI/logic based PHP, SQL, & JavaScript integration to help our client systems decide the best actions for users... An example:
- we collect data on features/products that our clients' customers & users enjoy
- we collect data on information that our clients' customers & users read
- we can show related features/products when the users access certain information
- we can show related info/articles when the users use certain features / products

We don't say "if user reads this, show this product" - we tell our AI to analyze what the user reads, what the user views/buys/wants, and make educated decisions based on overall analytics & data. It's not 100% AI, and it's not 100% hand-coded logic. it's a mix
We have obviously made more advanced AI for our systems & less advanced too, but the premise is this... You can create 100% AI bots that aren't given any ideas/strategies/outcomes by the programmers, you can create 100% logic scripts that never think for themselves, or you can create hybrids... And there's a place for all 3. It just depends on your needs
 
Last edited:
@Ghost pretty much explained in a nutshell php is not really able to do AI its possible but its not equipped to handle what it needs, Python is your best bet to start with AI but it is a very deep learning curve, a good friend of mine is looking into AI at the moment and he told me that the amount of learning and resources needed takes a lot so be prepared to sit down with plenty of coffee or tea as it will take a bit to get started. Their is some simple AI projects to get you started though but they are simple before you head into advanced deep learning.
 

New Threads

Buy us a coffee!

Back
Top Bottom