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:
- 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...
- use PHP to store every tic tac toe move to a database... record the full games from first/starting move to the end
- 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.
- 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
- 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