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

php basics, some things that i lost

i would understand better some things that in the videos i don't understood very well

1: stripos

2: isset

3: $_POST, GET and SERVER (SERVER REQUEST, i don't remember well)

4: trim

5: the difference between include, include_once, require, require_once

6: how to use -> (not => i got the array key)

7: where i need to use exit; because in general in js you use break for exemple in the infinite loops but i saw in some videos here of php basics that exit is used also outside of a loop to break something, i'm a bit confused because i guess that a function or a statement stop if it is not an infinite loop and i need the exit only where it needs

1 Answer

Codin - Codesmite
Codin - Codesmite
8,600 Points

stripos()

The stripos() function finds the position of the first occurrence of a string inside another string.

For example:

<?php
echo stripos("This is an example!, example");
?>  

This will return 11 as the string "example" starts at the 11th character in the string (The "T" in "This" is position 0).

You also have;

strripos() - Finds the position of the last occurrence of a string inside another string.

strpos() - Finds the position of the first occurrence of a string inside another string.

strrpos() - Finds the position of the last occurrence of a string inside another string.

strpos(), strrpos() and striipos() are case-sensitive whereas stripos() is case-insensitive.

isset()

The isset() function checks to see if a variable is set and is not NULL.

For example:

<?php
$test = 10;
if (isset($test)){
  echo "Test1 is set to " . $test;
}
else {
  echo "Test1 Is not set";
}


if (isset($test2)){
  echo " Test 2 is set to " . $test2;
}
else {
  echo " Test2 is not set";
}
?>

This will return "Test1 is set to 10 Test2 is not set" as Test1 is set to 10 and test2 has not been set.

$_POST, $_GET, $_SERVER

PHP superglobals $_POST and $_GET are used to collect form data.

For example:

Your Form

<html>
<body>

<form action="welcome.php" method="post">
Name: <input type="text" name="name"><br>
E-mail: <input type="text" name="email"><br>
<input type="submit">
</form>

</body>
</html>

The data submitted in your form can be called from welcome.php like so:

<html>
<body>

Welcome <?php echo $_POST["name"]; ?><br>
Your email address is: <?php echo $_POST["email"]; ?>

</body>
</html> 

$_GET is an array of variables passed to the current script via the URL parameters.

$_POST is an array of variables passed to the current script via the HTTP POST method.

$_SERVER is an array containing information such as headers, paths, and script locations. The entries in this array are created by the web server.

<?php echo var_dump($_SERVER); ?>

Will return the information contained in the array but not all webservers will return information or all of the information due to security settings.

trim()

trim() is a function used to remove whitespace from both sides of a string.

For example:

<?php
$example ="  This example contains white space at the start and end!  ";
echo trim($example);
?>

Will return "This example contains white space at the start and end!" rather than " This example contains white space at the start and end! " as trim() removed the white space from the start and end.

include, include_once, require, require_once

include and include_once will continue running the code if the include file is missing, where as requrie and require_once will exit if the file is missing as it is required to run the application.

require_once and include_once will only allow the file to be included or required once where as include and require allow you to include and require the file multiple times.

->

"->" Is used when reffering to a member of a PHP object.

exit()

exit() function is an alias for die() both stop the current script and can have a message returned.

For example:

<?php
echo "First String, ";
exit("The Second String will not be displayed as the script has been stopped!");
echo "Second String";
?>

Will display "First String, The Second String will not be displayed as the script has been stopped!".

Hope this helps :)

Thanks :)