DEV Community

PHP For JavaScript Developers

Megan Miller on May 01, 2020

I started learning how to code in 2012 with HTML and CSS. Ever since the start of my coding journey, PHP has been one of the languages I’ve always ...
Collapse
 
dog_smile_factory profile image
Dog Smile Factory

Learning PHP is a great choice, and I wouldn't be surprised if it's soon added to the list of technologies you love in your bio! For anyone interested, I would recommend PHP: The Right Way for solid advice on learning the language, as well as Laravel, a web application framework that makes coding with PHP a real pleasure. Best of luck with your exploration of PHP.

Collapse
 
mjcoder profile image
Mohammad Javed

Laravel +1

Collapse
 
lokidev profile image
LokiDev

Hey,

So I need to be the mean part here, I'm really sorry :/.

But there are a few "wrongs" in this article and I need to clarify

Syntax

While you CAN write ?> at the end of your files, you really SHOULDN'T. In Javascript there are multiple competing syntax-standards (like the one from AirBNB or the "Standard" standard.), but PHP is more like Python: There are PSR-2 and PSR-12 which describe exactly how standard-compliant code should look like. No more fights over indentation and so on ;).
php-fig.org/psr/psr-12/

Rules 2.2.:

The closing ?> tag MUST be omitted from files containing only PHP.

Emphasis on the "only".

Standards

Btw the next thing missing (maybe you didn't stumble upon it yet) are other references to the PSR. To step up your game, you really should try to understand all of them. But PLEASE refer from PSR-8, as it's the wrong time for now.

Declaring Variables

define doesn't really declare Variables, but compile time constants. They really can't be changed by any means and are like a little chipmunk replacing all the occurences with it's true value. This is the reason why you can't write sth. like:

define(POWERLEVEL, $sayajin->determinePowerlevel());
Enter fullscreen mode Exit fullscreen mode

What you CAN do is simple math:

define(POWERLEVEL, 2*9*500);
Enter fullscreen mode Exit fullscreen mode

Declaring Arrays

Not really about declaring arrays, but you say, that an Array is the only way to create a collection of key/value pairs without importing.
But you really can (and should) use data classes which hold your data and data collection. Like in javascript you can use them in parameters without their content to be copied by whole, but as a reference!

Functions

Nothing wrong here, but keep in mind, that all Objects are implicitly called by reference, while all scalars (int, string, arrays) are copied by value!

function applySomeProperty($someClass) {
    $someClass->bestBuddy = "Kuririn";
}

$unimportantClass = new class {};
applySomeProperty($unimportantClass);
echo $unimportantClass->newProperty; // Kuririn
Enter fullscreen mode Exit fullscreen mode

And while we're here: Since the 7.x Versions PHP supports types!

function someFunction(int $age, string $name): PersonObject {}
Enter fullscreen mode Exit fullscreen mode

You really should use them!

Classes

Not much here, but it's important to know, that you use :: in static context, while -> in private. Additionally there's a difference between $this::$varname and self::$varname.

Collapse
 
ziizium profile image
Habdul Hazeez

She said and I quote:

I'm still new to PHP

Collapse
 
lokidev profile image
LokiDev

So shes even more dependent on not learning sth. wrong.

How is someone supposed to learn and get better if everyone just sugar-coats you?

Thread Thread
 
ziizium profile image
Habdul Hazeez

So shes even more dependent on not learning sth. wrong.

How is someone supposed to learn and get better if everyone just sugar-coats you?

You might have missed the following comments and responses.

This comment:

Friendly reminder for a syntax shockingly difference between PHP and any other language. The dot ( . ) is the concatenation operator!

$hello = "hello";
$hello .= " world!";
echo $hello; // out: hello world!

For more info, check this out: positronx.io/php-concatenate-strin...

Her response:

I knew I was forgetting something, thank you!


Also this comment:

Setters are unnecessary in PHP. Use immutable private attributes and be happy.

Her response:

Thank you for the clarification!


And this:

Nice work Megan, just one thing: The spread operator is only available in PHP 7.4 everything else is awesome

And her response:

Thank you for the clarification! I'll add that in ASAP! 😀

Collapse
 
smoldev profile image
Megan Miller

I really appreciate this comment. Thank you so much! I'm still learning, so this is really helpful. 😊

Collapse
 
lokidev profile image
LokiDev

I really didn't want to be mean. I'm just the direct kind of guy. Another very helpful resource:
phptherightway.com/

This is a lot. Really. One chapter a week should be good enough to get you in the top 10% :).

Another tip: try to use tools like phpstan (or psalm) and php mess detector as early and strict as possible. They really force you to write better :)

Collapse
 
ziizium profile image
Habdul Hazeez

This brings back lots of memory from when I first learned PHP. Thank you.

When I find anyone who wants to know the similarities between JavaScript and PHP, I'll gladly point them to this post.

I'll bookmark it after hitting the submit button for this comment.

Collapse
 
upieez profile image
Samuel Huang

Awesome article! I started learning PHP awhile back and I always thought it has some similar style to JS. With you article, it confirmed my suspicion and learning about how classes work in PHP is so cool. Thank you for this :)

Collapse
 
smoldev profile image
Megan Miller

I'm so glad you enjoyed it!

Collapse
 
ansmtz profile image
ansmtz

PHP is awesome. Despite the rumours that it'll die soon.

Collapse
 
c0llinn profile image
Raphael Collin

Very nice article! You encouraged me to learn PHP.

Collapse
 
ziizium profile image
Habdul Hazeez

Can I make recommendations on resources to get you started?

Collapse
 
c0llinn profile image
Raphael Collin

Of course! Go ahead.

Thread Thread
 
ziizium profile image
Habdul Hazeez

Books

Website

Tools

Videos

*Security is important in Web application development and you should never consider it as an afterthought which is the main reason I included these resources.

Collapse
 
smoldev profile image
Megan Miller

I'm glad!

Collapse
 
dabretema profile image
Daniel Brétema

Friendly reminder for a syntax shockingly difference between PHP and any other language. The dot ( . ) is the concatenation operator!

$hello = "hello";
$hello .= " world!";
echo $hello; // out: hello world!

For more info, check this out: positronx.io/php-concatenate-strin...

Collapse
 
smoldev profile image
Megan Miller

I knew I was forgetting something, thank you!

Collapse
 
memitaru profile image
Ami Scott (they/them)

It's really cool seeing things laid out like this where I can compare them to JavaScript! Branching out into a new language always feels a little intimidating to me but seeing the JS and PHP side by side makes it a little less intimidating.

Collapse
 
smoldev profile image
Megan Miller

I'm glad you enjoyed it, that makes me so happy!

Collapse
 
vlasales profile image
Vlastimil Pospichal

Setters are unnecessary in PHP. Use immutable private attributes and be happy.

Collapse
 
smoldev profile image
Megan Miller

Thank you for the clarification!

Collapse
 
manuelojeda profile image
Manuel Ojeda

Nice work Megan, just one thing: The spread operator is only available in PHP 7.4 everything else is awesome

Collapse
 
smoldev profile image
Megan Miller

Thank you for the clarification! I'll add that in ASAP! 😀

Collapse
 
agitri profile image
agitri

Forget php and start with java, i do both and java is the better one 😊

Collapse
 
alexchadwickp profile image
Alex Chadwick Peribañez

I think most people would be inclined to agree, but there are times where you need to maintain legacy systems written in PHP and it doesn't make sense to spend time and resources migrating it to a different technology stack, so knowing PHP still comes in handy!

Collapse
 
devwarr profile image
Devin Warrick

This article is amazing. Nice and sweet, with code examples and explanations of what's going on. Anytime I hop to PHP I'll be looking at this article to brush up my skills.

Collapse
 
smoldev profile image
Megan Miller

I'm so glad you liked it!

Collapse
 
agitri profile image
agitri

Im talking about jdk and not js 😊

Thread Thread
 
lokidev profile image
LokiDev

And it's still wrong. There are business cases for java and others for PHP. Both are valid languages, but none is superior to the other.

Btw: Would you show me your goto java solution which doesn't need dependencies, takes an json-url, gets the content and pretty prints its content in a file?

$content = file_get_contents(URL);
$jsonObject = json_decode($content);
$beautifulJsonString = json_encode($jsonObject, JSON_PRETTY_PRINT);
file_put_contents(FILENAME, $beautifulJsonString);

15s - I really want to see doing that in Java :D.

Collapse
 
bingojosh profile image
Josh LaRochelle

This is absolutely brilliant and exactly what I needed for my upcoming job interview.