Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP Build a Simple PHP Application Working With Functions Introducing User-Defined Functions

If I define a variable within my head section in my HTML, how can I call it in other section within the same web page?

I am using a boolean var just to see if the user is getting a message but I want to use it again in other section of my code but I get an error msg that it is'nt defined.

5 Answers

Ryan Field
PLUS
Ryan Field
Courses Plus Student 21,242 Points

It's kind of hard to say without seeing your code, but you should be able to declare a variable in PHP (like in your index.php file) and call that variable later on in the page. Can you post what you have and where the issue is?

Sure.

<!DOCTYPE html> <html> <head> <?php //PHP CODE 3 $cuentaNueva=0; <----- I WANT TO CALL THIS VARIABLE $msg=""; if($_SERVER['REQUEST_METHOD']=='POST'){ if(!empty($_POST['email']) && !empty($_POST['pass']) && !empty($_POST['carrera']) && !empty($_POST['area'])) { ------ LOTS OF CODE OVER HERE --- ------SEVERAL SECTIONS----

<section id="suscribe" class="stepSusc about-us" data-midnight="full">

I want to use it over here and I get an error

<?php if($cuentaNueva==0) {?> <------------------IN THIS LINE <h1>Crea tu cuenta ahora.</h1> <form action="index.php#suscribe" method="post" class ="pure-form pure-form-stacked"> <fieldset>

Lol, It is not quite clear. Sorry.

Is over here:

if($_GET["cuentaNueva"]==0) IF I USE PHP BLOCKS IT DOESN'T APPEARS IN THE ANSWER <h1>Crea tu cuenta ahora.</h1> <form action="index.php#suscribe" method="post" class ="pure-form pure-form-stacked"> <fieldset>

$_GET["cuentaNueva"] is calling a $_GET variable from the URL were $cuentaNueva=0 is being set as a global you can not use $_GET to call a global variable. if you were wanting to do this it will have to be set as a $_GET variable. like so: example.php?cuentaNueva="variable here" and if($_GET["cuentaNueva"]==0) would work.as its being set globally you would want if($cuentaNueva==0) {do something} this would work.

But when I run it without get (this means if ($cuentaNueva==0) then it just returns an error that says that i had not been indexed or declared. I kind of understand the mean of global, private, public and that stuff because of my OOP school course, but, is there any course here in treehouse that explains it for php?

$_GET["cuentaNueva"] needs example.php?cuentaNueva="variable here" and if($cuentaNueva==0) {do something} needs $cuentaNueva=0 . your code from what i see you set $cuentaNueva=0 so you would need to use if($cuentaNueva ) {do something}.

$cuentaNueva=0;
$msg=""; 
if($_SERVER['REQUEST_METHOD']=='POST'){ 
  if(!empty($_POST['email']) && !empty($_POST['pass']) && !empty($_POST['carrera']) && !empty($_POST['area'])) {