Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Creating PHP Variables #3

Closed
github-learning-lab bot opened this issue Nov 11, 2020 · 1 comment
Closed

Creating PHP Variables #3

github-learning-lab bot opened this issue Nov 11, 2020 · 1 comment

Comments

@github-learning-lab
Copy link

Creating Variables

Building Variables

Variables are an essential part of any project. They hold data that can be modified or used later in a program.

We define variables in PHP with a $ sign and then we need to decide what the datatype for the variable will be. For a string of text make sure to put the text in quotes. If the value is a decimal or integer, no quotes are needed.

For Example, let's add these after the <?php in our code:

$name = "PHP Store";
$credit = 1000;

Combining text and Variables

Using a period (.), we can concatenate text and variables. Replace the echo line with the following:

echo "<h1>Welcome to ".$name."!</h1>";
echo "<h2>You have $".$credit." in your wallet.</h2>";";

Your code should look like this now:

<!DOCTYPE html>
<html>
  <head>
    <title>PHP Store</title>
  </head>
  <body>
    <?php
      $name = "PHP Store";
      $credit = 1000; 

      echo "<h1>Welcome to ".$name."!</h1>";
      echo "<h2>You have $".$credit." in your wallet.</h2>";
    ?>
  </body>
</html>

Refresh your browser and you should these sentences: Welcome to PHP Store! You have $1000 in your wallet.

If that worked, push your file to GitHub for the next step:

git add index.php
git commit -m"add variables"
git push origin master
@github-learning-lab
Copy link
Author

Awesome! You are one step closer to making dynamic PHP websites!

Click here to organize variables in arrays.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

0 participants