M. Niyazi Alpay
M. Niyazi Alpay
M. Niyazi Alpay

I've been interested in computer systems since a very young age, and I've been programming since 2005. I have knowledge in PHP, MySQL, Python, MongoDB, and Linux.

 

about.me/Cryptograph

PHP Variable Definition and If - Else structure

PHP Variable Definition and If - Else structure

Variable Declaration

In PHP, variables are defined with the $ sign.

$variable = variable value;

  • There are some rules for defining variables.
  • Variables cannot start with a number.
  • Turkish characters cannot be used.
  • Whitespace cannot be used.
  • It is case-sensitive.

A declaration like $variable_1 can be made, and any value can be assigned to the variable.

$add = (mysql_query("INSERT INTO TableName (Field1, Field2, ...) VALUES ('Value1', 'Value2', ...)");

When this variable is executed, it will add data to the database.

$write = "Alpay"; The value of the variable is Alpay. When we print this variable to the screen, it will display Alpay.

$write = "Alpay"; 
echo $write;
echo "My name is: ".$write;

This will output "My name is: Alpay" to the screen.
When printing variables, we do not use ' symbols. We use the . symbol to concatenate both normal text and variables simultaneously. . is a concatenation symbol.

echo 'My name is: '; echo $write;

This will also output the same result, but instead of using two echo commands, we concatenate the two values to be printed with the . symbol.

If - Else Structure

if (conditions you want to be met) {
code to be executed when the condition is met
}

elseif (checks this condition if the previous condition is not met) {
Code to be executed when the condition is met
}

else {code to be executed when the condition is not met}

Example:
A program that gives the incorrect warning if the value entered from the outside is 1.

$Alpay = "1";
if($Alpay == "1") {
  echo 'The entered value is correct.';
} 
else {
   echo 'The entered value is incorrect.';
}

You may also want to read these

There are none comment

Leave a comment

Your email address will not be published. Required fields are marked *