how can i use if statemend with strings in php?
and example is the following code:

<?php
$url = $_GET['url'];
$length = strlen($url);
$url = explode('/', $url);
print_r($url);
echo "<BR>";
if($length >= 0){
    echo "URL excits";
}
if($url[0] = 'home'){
    echo $url[0];
}

but whenever i pass a parameter in the url, it displays home.. what is wrong?

i spent almost a year now without using php so i really nred your help. thanks for anyone who will try to help me.. :)

Member Avatar for diafol

Can you give an example of the type of string that $_GET['url'] is supposed to pass?

$url = $_GET['url'];

May give an error if it's not set in the url querystring. Something more like this?

if(isset($_GET['url'])){
    $url = explode('/', $_GET['url']);
    print_r($url);
    echo "<BR>";
    echo "URL excits";
}else{
    $url[0] = 'home';
    echo $url[0];
}
> ($_GET['url'])){
>     $url = explode('/', $_GET['url']);
>     print_r($url);
>     echo "<BR>";
>     echo "URL excits";
> }else{
>     $url[0] = 'home';
>     echo $url

$url is supposed to hold a string defined in the url.. like a page name.. e.g.: home, about, contact.. etc..

the code is supposed to include the page though home.php
it would be something like:

include $url.".php";

so by assuming that the url contains http://localhost/index.php?url=home
the code should see if the page is home and include it and at the same time have a complete list of things to do
example:

if($url = "home")
{
set_headers("title", "home");
echo "welcome to the home page!!";
include $url.".php"; // for mre functions..
}

but for sure, the url will not be handling integers.. if i was to handle intergers i would have used == or === but these $url valies are completely letters or text

hey! i got the solution.. i was supposed to make == instead of =
the final code is:

<?php
$url = $_GET['url'];
$length = strlen($url);
$url = explode('/', $url);
print_r($url);
echo "<BR>";
if($length >= 0){
    echo "URL excits";
}
if($url[0] == 'home'){
    echo $url[0];
}
Member Avatar for diafol

I'd do an Apache rewrite in .htaccess file to write the url as path:

http://www.example.com/home/ or http://www.example.com/about/

RewriteEngine On
RewriteRule ^([^/]*)/$ /index.php?url=$1 [L]

Then

if(isset($_GET['url'])){
    $get = $_GET['url'];
    $load = (file_exists($get.php)) ? "$get.php" : "home.php"; // use home or 404 page and change the paths if the include file is in a subdirectory
}else{
    $load = "home.php";
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.