In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
In this issue, the editor will bring you a quick start guide to PHP for JavaScript developers. The article is rich in content and analyzed and described from a professional point of view. I hope you can get something after reading this article.
I've been learning how to code in HTML and CSS since 2012, and PHP has been one of the languages I've always wanted to learn since the beginning of my coding journey.
As a student who just graduated from JavaScript training school, I decided to try PHP again. It has always been difficult to find resources about PHP, rather than for people who have never been exposed to code. So, that's why I wrote this article. I hope to help other people like me who just need a quick guide to the difference between the language they choose and the language they want to learn.
General grammar
One of the big differences between PHP's syntax and JavaScript's is that PHP's syntax requires a semicolon at the end of the line. I was obsessed with this problem at first, and sometimes now, so I would like to explain it here first.
Use language
In JavaScript, in addition to ensuring that the file has a .js extension, you do not need to do anything special to run the code. However, in PHP, even in files with a .php extension, you need to use tags.
Declare variable
Creating variables in PHP is very simple. Like JavaScript, PHP is a dynamically typed language, so it is not necessary to declare the type of a variable when it is created. It uses the $symbol to represent variables.
$myvar = 'value'
In PHP, any variable you declare is mutable by default and can be changed anywhere.
Declaration constant
PHP has a special feature called define that specifically creates variables that cannot be changed. It takes two parameters: the name of the variable and the value to assign to it. By default, this function sets the name of the variable you create to case-sensitive. You can override it by passing true as the third argument to the function.
Define ('CONSTANT_NAME', value, true)
Declare array
Arrays much like JavaScript can be created with standard parenthesis symbols or a function in PHP. In other words, PHP's associative array is the equivalent of a JavaScript object and is the only way to create a collection of key/value pairs in PHP without importing a module. Key assignments in PHP are represented by = >.
$myArray = ['key1' = >' value', 'key2' = >' value', 'key3' = >' value']
Function
The functions in PHP are very similar to JavaScript (especially ES5).
Function myFunc ($param) {return $param;}
In this respect, the only real difference I can find between the two languages is that PHP has an operator that changes the parameters you pass in from value-based to reference: &.
$myVar = 10; echo $myVar; # displays 10 function addTen (& $param) {return $param + = 10;} addTen ($myVar); echo $myVar; # displays 20
Cycle
Like functions, loops are not much different from how they are written in JavaScript. One exception is PHP's foreach loop, which varies depending on the type of array you are trying to loop.
Foreach ($arrayName as $item) {# do code} class and OOP methodology
Class is that PHP is very different from JavaScript. Although PHP was not originally used as an object-oriented programming language (similar to JavaScript), it was later added.
Access modifier keyword
In standard JS, classes do not require modifiers. But for PHP, they are.
The modifiers you have in PHP are:
Public-- can use it outside the class through scripts or other classes.
The class that private-- creates this class is the only class that can access it.
Protected-it can be accessed outside the class only if it is called in a subclass of the class.
Static-allows the use of a property or method without instantiating the class to which the property or method belongs.
When creating a class in PHP, it is a good practice to use these keywords to tell the class how it needs to use the properties and methods in the class.
Class MyClass {private $classAttrib; public function _ _ construct ($classAttrib) {this- > classAttrib = $classAttrib;}}
In the code snippet above, you will notice a few things. First of all, there may be two modifier keywords. Here, we declare a private variable named classAttrib, which can only be accessed through MyClass. The second is the public keyword that we use in conjunction with PHP's built-in _ _ construct method. As in JavaScript, this allows us to instantiate a class as if it were a function.
$myClass = new MyClass (someValue)
This and arrowhead
Continuing with the MyClass example above, you will notice that we use it in the same way as in JavaScript. The difference here is that we use the arrow (- >) to access the classAttrib on this class. We will also use this pointer to access anything on the class that needs to be used throughout the code.
This is the same class in JavaScript:
Class MyClass {constructor (classAttrib) {this.classAttrib = classAttrib;}}
Getters and Setters
Getter and Setter are class methods used to get and set (or update) information about class properties. In JavaScript, we don't usually need to write them, and similarly, we don't need them in PHP. Having said that, you will see that these methods are much more frequent in PHP, so I think it's better to be cautious here. Basically, these methods are the only things that should be modified directly outside the class or interact with class properties.
#... Inside MyClass public function setClassAttrib ($classAttrib) {return $this- > classAttrib = $classAttrib;} public function getClassAttrib () {return $this- > classAttrib;}
Inherit
Inheriting from a parent class in PHP is similar to JavaScript, except that we do not use super to pass the properties of the parent class. Instead, we use the:: operator, also known as the range parsing operator.
Class SecondClass extends MyClass {private $newAttrib; public function _ construct ($classAttrib, $newAttrib) {parent::__construct ($classAttrib); this- > newAttrib = $newAttrib;}} similarities between PHP and JavaScript
Now that we've discussed some of the differences between JavaScript and PHP, let's talk about some similarities!
PHP has expanded (...) Grammar! You can use exactly the same syntax as in JavaScript, both in parameters (parameters unpacked above PHP 5.6) and arrays (starting with PHP 7.4 and above)!
PHP has ternary operators!
PHP has a cast of = =!
This is what the quick start guide to PHP written for JavaScript developers is like. If you happen to have similar doubts, please refer to the above analysis to understand. If you want to know more about it, you are welcome to follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.