← PHP tutorial

PHP

Variables

Every PHP variable name starts with $: $name = "Ada";. Like Python or JavaScript, you don't declare a type, it's inferred from whatever value you assign.

Example: variables inside a string

<?php
  $name = "Ada";
  $age = 21;
  echo "$name is $age years old.";
?>
Ada is 21 years old.

Variables placed directly inside a double-quoted string ("$name") are automatically substituted with their value, this only works inside double quotes; single-quoted strings ('$name') print the literal text $name instead, with no substitution at all. That distinction catches a lot of beginners the first time they switch quote styles without thinking about it.

Example
<?php
  $name = "Ada";
  $age = 21;
  echo "$name is $age years old.";
?>
Output
Ada is 21 years old.
Ready to go further?

These free tutorials are a preview, our Backend Development cohort covers it in depth, with a real project and a cohort alongside you. Closes Nov 30

Enroll in Backend Development

Cookie preferences

Choose what you're comfortable with. You can change this any time from the footer.

Don't miss out

Ready to build something real?

Create a free account to enroll in a track, and get notified the moment a cohort opens.

Create free account