Recommended Articles. Article Contributed By :. Easy Normal Medium Hard Expert. Writing code in comment? Please use ide. Load Comments. What's New. Most popular in PHP. How to execute PHP code using command line? Most visited in PHP Programs. How to call PHP function on the click of a Button? We use cookies to ensure you have the best browsing experience on our website.
Start Your Coding Journey Now! Login Register. We are starting off with the basics — how to declare PHP in a file, write comments, and output data.
PHP files end in. Consequently, you can execute PHP on a page:. Like many other languages, PHP also has the ability to add comments. In PHP, you have several ways for that:. In PHP, data is commonly output using echo or print. For example, the title of this blog post might be displayed on a page like this:.
The two commands echo and print are pretty much the same. The only difference is that the former has no return value and can take several parameters, while the latter has a return value of 1 and can only take one argument.
An important note: Like all other PHP commands, functions echo and print are not case sensitive. Functions are shortcuts for commonly used chunks of code. Instead, you create them once and use the shortcuts when you need them. Much of this PHP cheat sheet is devoted to that. Quick explanation: the first part is the function of a name reminder: function names are not case sensitive. After that, everything between the curly braces is what the function does when called.
Similarly to most other programming languages, PHP lets you work with variables and constants. These are pieces of code that store different kinds of information. To do anything with variables, you first need to define them.
A typical example:. There is no need to declare PHP variables in a certain way. They automatically take on the type of data they contain. Variables can be available in different scopes, meaning the part of a script you can access them. This can be global , local and static. Any variable declared outside of a function is available globally. That means it can be accessed outside of a function as well.
If you declare a variable inside a function, it will have a local scope. The consequence is that it can only be accessed within that function. A way around this is to prepend a local variable with global. That way, it becomes part of the global scope. PHP also comes with a number of default variables called superglobals. Aside from variables, you can also define constants which also store values.
Constants are useful since they allow you to change the value for an entire script in one place instead of having to replace every instance of it. They are also global in nature, meaning they can be accessed from anywhere. Arrays are a way to organize several values in a single variable so that they can be used together. While functions are for blocks of code, arrays are for the values — a placeholder for larger chunks of information.
In programming, speech strings are nothing more than text. As we have settled earlier, they are also a valid value for variables. Operators allow you to perform operations with values, arrays, and variables. There are several different types. You can use the sign to prevent expressions from generating error messages. This is often important for security reasons , for example, to keep confidential information safe.
These are not single-quotes! PHP will attempt to execute the contents of the backticks as a shell command. Loops are very common in programming.
They allow you to run through the same block of code under different circumstances. PHP has several different ones.
The final PHP loop runs a code snippet once, then repeats the loop as long as the given condition is true. They are statements for running code only under certain circumstances. You have several options:. PHP is often used for handling web forms. Both are able to catch values from input fields, however, their usage differs. That means all variable names and their values are contained in the page address. Keep in mind that it also means that the information is visible to everyone.
For that reason, GET is not suitable for sensitive information such as passwords. It also limits the amount of data that can be sent in ca characters. This makes the data invisible to third parties, as it is sent in the HTTP body.
You are not able to bookmark it. With POST, there are no limits to the amount of information you can send.
0コメント