r/PHPhelp • u/v1adxyz • Mar 23 '23
PHP vs Python
PHP and Python are popular programming languages but have different strengths and weaknesses. So which one should I choose from?
6
u/itemluminouswadison Mar 23 '23
i do both daily and i prefer php, i'd think. it's a little more classically OOP. less behind the scenes magic. init files that do things you weren't aware of. also, docstrings under the class name (wtf) vs php docblocks on top of the class name
can't go wrong with either one, though.
think about where you'd wanna see yourself in 5 years or something and look at the job market for what languages they're hiring for that. that's the most important thing imo
4
u/hedrumsamongus Mar 23 '23
PHP's my primary, and I've been getting into Python a bit more recently to do serverless stuff. I find that the PHP ecosystem is much simpler to get up and running with, and I like that the structure of the application is defined by where you tell Composer your namespaces are and where you tell your webserver that the public root directory is. All very explicit, Composer's dead simple to set up and does so much heavy lifting for you! I'm still a bit confounded by how difficult it is to get Python to import a module that I've written (but not packaged and installed) located in a sibling directory.
Either language you pick can get most jobs done, of course. The real question from a career standpoint is whether someone will pay you to do job X with language Y.
1
16
u/jonnyclueless Mar 23 '23
I prefer PHP mainly because I don't like spaces controlling how the code is executed. Some people may prefer that and not like having brackets and things like that. Though if you are doing any machine learning, Python has one of the largest community libraries for that.
0
Mar 23 '23
[deleted]
1
2
u/cursingcucumber Mar 23 '23
I have apples and oranges, both are fruit but some people like oranges and some like apples. Which should I choose? 😂
2
2
u/equilni Mar 25 '23
You are asking in a PHP forum, so of course the answer is PHP. Honestly, like others have noted, it depends on what you are doing.
but have different strengths and weaknesses
If I recall correctly, PHP's OOP is more verbose than Python.
Classes:
PHP:
class Noun {
private string $Attribute = '';
public function Verb(): string {
return $this->Attribute;
}
}
$noun = new Noun();
echo $noun->Verb();
Python:
class Noun:
Attribute = ''
def Verb(self): #python passes self to methods, except for static
return self.Attribute
noun = Noun()
print(noun.Verb())Visibility. If I recall for Python, it's just a naming convention, not enforced by the system.
PHP
Public = public $name
Protected = protected $name
Private = private $name
Python
Public = name
Protected = _name (single underscore)
Private = __name (double underscore)Static methods:
# PHP
class Test {
public static function welcomeMessage() {
return 'Welcome!';
}
}
echo Test::welcomeMessage();
# Python
class Test:
@staticmethod
def welcomeMessage(): # don't have to pass self
return 'Welcome!'
print(Test.welcomeMessage())Inheritance. Python reminds me of using DI in PHP, which isn't that far off...
# PHP
Class Test {}
Class Test2 extends Test {}
# Python
class Test
class Test2(Test)Multiple inheritance. Again, Python acts like DI with PHP.
PHP - Cannot be done. Use Traits
trait Test1 {}
trait Test2 {}
class TestOne {}
class TestTwo extends TestOne {
use Test1, Test2;
}
Python
class One:
os = 'Mac OS'
class Two:
brand = 'Apple'
class Three(One, Two):
def __init__(self):
print(self.os) # Mac OS1
u/Rukarumel Mar 23 '23
Python. PHP is a very good language, I used to be a PHP programmer. But in these days you’ll have more jobs with Python
1
Mar 25 '23
PHP is great for web and that is what I use. I use something else for other things though.
6
u/tridd3r Mar 23 '23
what do you plan on doing with the language?